[RESOLVED] Updating a label on Button Clicks
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Updating a label on Button Clicks

  1. #1
    Join Date
    Nov 2011
    Posts
    3

    [RESOLVED] Updating a label on Button Clicks

    Hello,

    My application has a simple textbox for user input, a database connection, a button and a label.

    I will search for the user input in my database and update the label when the user clicks the button.

    This works fine on the first button click but ceases to function on subsequent clicks.

    Please check out the attached image to get an overview of what I have. Imagine that a user will enter her/his name in the text box, I will check if the name exists in the database and display it on the label...then the user will enter another name etc...

    I would love to learn best practices as well...so please let me know if some of my code should belong in a new code file...in a separate class...maybe I can make use of fewer try-catches etc.
    Code:
        public partial class Form1 : Form
        {
        SqlConnection con;
        DataSet ds1;
         
        public Form1()
        {
        InitializeComponent();
        }
         
        private void Form1_Load(object sender, EventArgs e)
        {
        con = new SqlConnection(...MyConnectionString...);
        ds1 = new DataSet();
        }
         
        private void label1_Click(object sender, EventArgs e)
        {
        //Dont really need this and will delete it later...Please ignore
        }
         
        private void button1_Click(object sender, EventArgs e)
        {
        try
        {
        con.Open();
        }
        catch(Exception ex)
        {
        Console.WriteLine("Con Open(): \n {0}", ex);
        return;
        }
         
        string sql = select a,b,c from table where x=y;
         
        SqlDataAdapter da = new SqlDataAdapter(sql, con);
         
        try
        {
        da.Fill(ds1, "MyDataSetName");
        }
        catch (Exception ex)
        {
        Console.WriteLine("Da Fill(): \n {0}", ex);
        return;
        }
        GetRecords();
         
        con.Close();
        }
         
        private void GetRecords()
        {
        DataRow dRow = ds1.Tables["MyDataSetName"].Rows[0];
        label2.Refresh();
        label2.Text = dRow.ItemArray.GetValue(0).ToString() + " " + dRow.ItemArray.GetValue(1).ToString() + " " + dRow.ItemArray.GetValue(2).ToString();
        }
        }
    Regards,

    MC
    Attached Images Attached Images  

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Updating a label on Button Clicks

    Have you used the debugger at all? I am betting that the second invocation is throwing an exception and, since you're essentially doing nothing about it and just merrily chuiging along, you don't even notice.

    Using Console.WriteLine() in a WinForms app is pretty useless unless you really just want to log to the output window under the IDE.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Nov 2011
    Posts
    3

    Re: Updating a label on Button Clicks

    I looked at the debugger briefly and did not see any exceptions being thrown.
    I thought it might have something to do with repaint labels or "Unfill" the data adapter that I am "Fill"ing...

    I will try to find out whats wrong now that I know my approach is indeed right.

    Thanks for your response!

  4. #4
    Join Date
    Dec 2011
    Posts
    61

    Re: Updating a label on Button Clicks

    reset (clear) your ds1 each time you click button1 before filling it with new values

  5. #5
    Join Date
    Nov 2011
    Posts
    3

    Smile Re: Updating a label on Button Clicks

    Thanks. That fixed it!

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center