CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2008
    Posts
    22

    Exclamation 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

  2. #2
    Join Date
    Feb 2008
    Posts
    22

    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,

  3. #3
    Join Date
    Feb 2008
    Posts
    22

    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,

  4. #4
    Join Date
    Feb 2008
    Posts
    79

    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

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    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.

  6. #6
    Join Date
    Feb 2008
    Posts
    22

    Re: Loop through database results and display

    Quote 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

  7. #7
    Join Date
    Feb 2008
    Posts
    79

    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

  8. #8
    Join Date
    Feb 2008
    Posts
    22

    Re: Loop through database results and display

    Quote 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?

  9. #9
    Join Date
    Feb 2008
    Posts
    79

    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

  10. #10
    Join Date
    Feb 2008
    Posts
    22

    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,

  11. #11
    Join Date
    Feb 2008
    Posts
    79

    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

  12. #12
    Join Date
    Feb 2008
    Posts
    22

    Re: Loop through database results and display

    Quote 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,

  13. #13
    Join Date
    Feb 2008
    Posts
    79

    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

  14. #14
    Join Date
    Feb 2008
    Posts
    22

    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,

  15. #15
    Join Date
    Feb 2008
    Posts
    79

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured