|
-
February 22nd, 2008, 06:38 PM
#1
Loop through database results and display
Hello,
I need some help getting a button into my results that i have SELECTED from a Table. I would like to be able to add a button, so if there was 10 results their would be 10 buttons; 1 in each row. I dont need any onclick events right now as i can sort that later, i just need to position the button, i have tried but it just errors.
Here is the code so far, i have marked where i would like the button.
Button -
Code:
private void InitializeMyButton()
{
Button button1 = new Button();
button1.Text = "Button";
Controls.Add(button1);
}
SQL Loop
Code:
private void form_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<body bgcolor='#f0f0f0'><font size='1pt'><table><tr><td>Name</td><td>Description</td><td>Preview</td><td>Button</td></tr>");
MySqlConnection myConnection = new MySqlConnection();
myConnection.ConnectionString = "*******";
myConnection.Open();
string mySelectQuery = "SELECT * FROM tabe";
MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
MySqlDataReader myReader;
myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
while (myReader.Read())
{
sb.AppendLine("<tr><td>" + myReader.GetString(0) + "</td><td>" + myReader.GetString(1) + "</td><td><img src='" + myReader.GetString(2) + "'/></td><td>" //BUTTON HERE\\"</td></tr>");
string load = myReader.GetString(3);
}
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
myConnection.Close();
sb.AppendLine("<table/></font></body>");
webBrowser1.DocumentText = sb.ToString();
}
}
}
I get errors like the + is not valid with void or string.
Thanks, PS - im using the latest .NET Framework
-
February 23rd, 2008, 02:39 PM
#2
Re: Loop through database results and display
Anyone?
I shall go into more detail as that will probably help.
I am wanting to dynamically create buttons; the number of buttons based on the number of results received from an SQL query. For example, 10 rows of information received, then there will be a button for each row.
Like this
Code:
sb.AppendLine("<tr><td>" + myReader.GetString(0) + "</td><td>" + myReader.GetString(1) + "</td><td><img src='" + myReader.GetString(2) + "'/></td><td>" //BUTTON HERE\\"</td></tr>");
I have marked where i would like a button, The code for generating a button is in my previous post, although when i add " + InitializeButton() + " it doesent work.
Please help,
Thanks,
-
February 24th, 2008, 05:36 AM
#3
Re: Loop through database results and display
I think i may need to use a foreach loop for this , i have been testing it words and it seems to be what i am after, although i think the problem is with the Control.Add attribute where it is like this Control.Add(Button1), this way the for each loop it is only adding one button because it can only have one button with that name, so how would i make each button individual? but keep the click propery to be the same, ie if there is 100 results the is not 100 onclick attributes but just the one.
Thank you,
-
February 24th, 2008, 02:25 PM
#4
Re: Loop through database results and display
will this work for you
Code:
//psudocode...
void MakeButtons(MySqlDataReader myReader)
{
int index = 0;
foreach (Row row in myReader)
{
sb.AppendLine("<tr><td>" + myReader.GetString(0) + "</td><td>" + myReader.GetString(1) + "</td><td><img src='" + myReader.GetString(2) + "'/></td><td>" +newButton(row, index++)+"</td></tr>");
}
}
//returns string for a newbutton
//all buttons have the same event callback
//all buttons have a unique name made from the word button and buttonNum
//call this in a for loop passing in the index of the loop
string newButton(DataRow row, int buttonNum)
{
return "<BUTTON type=\"button\" name=\"button\""+buttonNum+"onclick=\"onClickEvent\">"+buttonTextProbablyGottenFromRow+"</BUTTON>";
}
Cheers,
Jon
-
February 24th, 2008, 02:46 PM
#5
Re: Loop through database results and display
sb.AppendLine("<tr><td>" + myReader.GetString(0) + "</td><td>" + myReader.GetString(1) + "</td><td><img src='" + myReader.GetString(2) + "'/></td><td>" //BUTTON HERE\\"</td></tr>");
If you're going to use a stringbuilder, use it properly:
Code:
sb.Append ("<tr><td>")
sb.Append (myReader.GetString(0));
sb.Append ("</td><td>");
sb.Append (myReader.GetString(1));
sb.Append ("</td><td><img src='");
sb.Append (myReader.GetString(2));
sb.Append ("'/></td><td>");
sb.Append ( //BUTTON HERE\\);
sb.Append ("</td></tr>");
There are 8 (or so) string concatenations in the original version, and zero in the second. The whole point of a stringbuilder is to avoid unnecessary string concatenations. Let the string builder deal with all the concatentations, that's what its good for!
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
February 24th, 2008, 03:57 PM
#6
Re: Loop through database results and display
 Originally Posted by jon.borchardt
will this work for you
Code:
//psudocode...
void MakeButtons(MySqlDataReader myReader)
{
int index = 0;
foreach (Row row in myReader)
{
sb.AppendLine("<tr><td>" + myReader.GetString(0) + "</td><td>" + myReader.GetString(1) + "</td><td><img src='" + myReader.GetString(2) + "'/></td><td>" +newButton(row, index++)+"</td></tr>");
}
}
//returns string for a newbutton
//all buttons have the same event callback
//all buttons have a unique name made from the word button and buttonNum
//call this in a for loop passing in the index of the loop
string newButton(DataRow row, int buttonNum)
{
return "<BUTTON type=\"button\" name=\"button\""+buttonNum+"onclick=\"onClickEvent\">"+buttonTextProbablyGottenFromRow+"</BUTTON>";
}
Hello, thanks for the reply,
But, when i replace my code with yours i get errors, i think its due to the { and }'s. where in my Windows Forms Application would i put this code and how would i use it?
Thanks
-
February 24th, 2008, 06:58 PM
#7
Re: Loop through database results and display
I was just offering psudocode
it wont compile as i wrote it
it sounds like you almost had it
you just need to build a button in html, like in the code i sent, instead of in c#, like your code was doing.
the rest looked ok
this code would be placed in the codebehind and called during some kind of postback to create the html page...
and yes... use a string builder....
Cheers,
Jon
-
February 25th, 2008, 02:04 PM
#8
Re: Loop through database results and display
 Originally Posted by jon.borchardt
I was just offering psudocode
it wont compile as i wrote it
it sounds like you almost had it
you just need to build a button in html, like in the code i sent, instead of in c#, like your code was doing.
the rest looked ok
this code would be placed in the codebehind and called during some kind of postback to create the html page...
and yes... use a string builder....
Yes that's it! thank you so much!, i used html in the webbrowser control on my Win Form, however i have my click command made up but i have never created an click command that is the result of an html button, only the windows.button. How would i do this?
-
February 25th, 2008, 02:08 PM
#9
Re: Loop through database results and display
Do you need to use the webcontrol?
this is easy to do in winforms
if you have to do web
you either need to add a javascript for the buttons to call... onClikc="javascriptMethodName"
or
you need to use a button with a runat="server" attribute so it can access an onclick method in c#.
Can you give more clarity on what your requirements are, so we can better help?
Cheers,
Jon
-
February 25th, 2008, 02:28 PM
#10
Re: Loop through database results and display
What the webBrowser in my form does is select information form my MySQL database and print it all out, there is a url that is selected from the database (a file the user can download). This is then put into a global variable. The button in every row should open a new form, where i then proccess the information in the url and download the file.
Heres the code.
The onclick
Code:
private void button1_Click(object sender, EventArgs e)
{
form2 form2 = new form2();
form2.Show();
}
You can find the code i use for downloading the file at the DownloadFile section on MSDN.
Code:
private void ccougar_Load(object sender, EventArgs e)
{
string variable2 = Global.variable;
StringBuilder sb = new StringBuilder();
sb.AppendLine(variable2);
textBox1.Text = Global.variable;
string remoteUri = Global.variable;
string fileName = Global.variable2, myStringWebResource = null;
WebClient myWebClient = new WebClient();
myStringWebResource = remoteUri + fileName;
myWebClient.DownloadFile(myStringWebResource, fileName);
// Create the file and clean up handles.
webBrowser1.DocumentText = sb.ToString();
if (Global.variable == "")
{
MessageBox.Show("Cannot locate file to download.", "Download Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
MessageBox.Show("Download Complete...", "Download Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Close();
}
Thanks for the help,
-
February 25th, 2008, 02:36 PM
#11
Re: Loop through database results and display
Why are you using the webbrowser to display data, rather than just a form?
Can the whole ui be in a form, and no webbrowser?
Can we just but buttons in the form, instead of in the webBrowser?
Cheers,
Jon
-
February 25th, 2008, 02:40 PM
#12
Re: Loop through database results and display
 Originally Posted by jon.borchardt
Why are you using the webbrowser to display data, rather than just a form?
Can the whole ui be in a form, and no webbrowser?
Can we just but buttons in the form, instead of in the webBrowser?
I dont know how to use datagrids with mysql though, that is why i used the webBrowser. Is there a way to do this without the webBrowser?
Thank You,
-
February 25th, 2008, 02:46 PM
#13
Re: Loop through database results and display
yes
psudocode...
Code:
get datareader from mysql connection
for each row in datareader
{
create a c# button
set its event delegate
set its position
add it to the form
}
then, on the form
create an onClick event for the buttons to reference
i can write this up, but i don't want to do it if there is some reason you need a webbrowser
if you can give me a datareader, i can give you a form...
Cheers,
Jon
-
February 25th, 2008, 02:51 PM
#14
Re: Loop through database results and display
Hello,
Thank you very much, this code connects to the database and gets the information.
Code:
MySqlConnection myConnection = new MySqlConnection();
myConnection.ConnectionString = "*************";
myConnection.Open();
string mySelectQuery = "SELECT * FROM table";
MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
MySqlDataReader myReader;
myReader = myCommand.ExecuteReader();
Then currently its a while loop like so
Code:
while (myReader.Read())
{
So i would need to change this to a foreach?
Could you provide an example of how i would print the data out onto the form and not onto the webBrowser?
Thanks very much,
-
February 25th, 2008, 03:09 PM
#15
Re: Loop through database results and display
This may or may not compile... but it will give you the idea
Code:
void InitX()
{
//SqlConnection
MySqlConnection myConnection = new MySqlConnection();
myConnection.ConnectionString = "*************";
myConnection.Open();
string mySelectQuery = "SELECT * FROM table";
MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
MySqlDataReader myReader;
myReader = myCommand.ExecuteReader();
System.Drawing.Point butonPos = new System.Drawing.Point(0,0);
int index = 0;
while(myReader.Read())
{
//using the data from the reader create a button
Button button = new Button();
button.Text = dr.GetString(0);//or wherever the data is in your reader
button.Tag = "put whatever data here, as an object, you need to pass to event";
button.Click+=new EventHandler(button_Click);// assign event
this.Controls.Add(button); //add button to form
button.Location = butonPos;
//if you also want to create other info on the form, now is the time to do it
//example
Label lab = new Label();
lab.Text = "yo" + index++;
this.Controls.Add(lab); //add button to form
lab.Location = new Point(200, buttonPos.Y);
butonPos.Y += button_Click.Height * 1.5;//set place for next button to create
}
}
void button_Click(object sender, EventArgs e)
{
Button senderButton = (Button)sender;
string buttonTagString = (string)senderButton.Tag; <--- this is how you can GetChildAtPointSkip data from the button
//do something
MessageBox.Show(buttonTagString);
}
Cheers,
Jon
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|