Link to home
Start Free TrialLog in
Avatar of Moti Mashiah
Moti MashiahFlag for Canada

asked on

C# code

Hi guys ,

I wrote some button code to updating data column.

Actually, what I'm trying to do is when I click the Button update it will copy over what I selected in the select status dropbox to the textbox Current status then update the column.

here is my code and also I attached screenshot.

//update button updating statu send email
        private void button1_Click(object sender, EventArgs e)
        {

            string conn = "Data Source=srv-db-02;Initial Catalog=rmsmasterdbtest;Persist Security Info=True;User ID=test;Password=masterB4";
            string Query = "update rmsmasterdbtest.dbo.serial set serialnumber='" + this.txtsn.Text + "' ,serialnumber3='" + this.txtstatus.Text + "' where serialnumber='" + this.txtsn.Text + "' ;";

            
            SqlConnection Myconn = new SqlConnection(conn);
            SqlCommand cmd = new SqlCommand(Query, Myconn);
            SqlDataReader Reader;

            try
            {
                if (combStatus.SelectedIndex == 0)
                {
                    txtstatus.Text = "OK";
                }
                if (combStatus.SelectedIndex == 1)
                {
                    txtstatus.Text = "Damaged";
                }
                if (combStatus.SelectedIndex == 2)
                {
                    txtstatus.Text = "Repair in Progress";
                }
                if (combStatus.SelectedIndex == 3)
                {
                    txtstatus.Text = "Repaired";
                }
                if (combStatus.SelectedIndex == 4)
                {
                    txtstatus.Text = "Loaner";
                }

                combStatus.Items.Add(txtstatus.Text);



                try
                {
                    Myconn.Open();
                    Reader = cmd.ExecuteReader();
                    MessageBox.Show("Updated Status");



                    while (Reader.Read())
                    {

                    }




                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Myconn.Close();
            }

Open in new window

Capture.JPG
Avatar of Jerry Miller
Jerry Miller
Flag of United States of America image

Are you getting error messages or is it not updating the data?

The one thing that I see is that you should test the textbox values (this.txtsn.Text  for example) to make sure that they have a value before sending them in a query.
Avatar of Moti Mashiah

ASKER

I don't get any error message I just have to click twice on the update button to get it update in the database. the first click don't update the database.

 string conn = "Data Source=srv-db-02;Initial Catalog=rmsmasterdbtest;Persist Security Info=True;User ID=test;Password=masterB4";
            string Query = "update rmsmasterdbtest.dbo.serial set serialnumber='" + this.txtsn.Text + "' ,serialnumber3='" + this.txtstatus.Text + "' where serialnumber='" + this.txtsn.Text + "' ;";

            
            SqlConnection Myconn = new SqlConnection(conn);
            SqlCommand cmd = new SqlCommand(Query, Myconn);
            SqlDataReader Reader;

           
                if (combStatus.SelectedIndex == 0)
                {
                    txtstatus.Text = "OK";
                }
                if (combStatus.SelectedIndex == 1)
                {
                    txtstatus.Text = "Damaged";
                }
                if (combStatus.SelectedIndex == 2)
                {
                    txtstatus.Text = "Repair in Progress";
                }
                if (combStatus.SelectedIndex == 3)
                {
                    txtstatus.Text = "Repaired";
                }
                if (combStatus.SelectedIndex == 4)
                {
                    txtstatus.Text = "Loaner";
                }

                combStatus.Items.Add(this.txtstatus.Text);



                try
                {
                    Myconn.Open();
                    Reader = cmd.ExecuteReader();
                    MessageBox.Show("Updated Status");

Open in new window

Can you try with this one?
//update button updating statu send email
        private void button1_Click(object sender, EventArgs e)
        {

            string conn = "Data Source=srv-db-02;Initial Catalog=rmsmasterdbtest;Persist Security Info=True;User ID=test;Password=masterB4";

            SqlConnection Myconn = new SqlConnection(conn);

            
            try
            {
                if (combStatus.SelectedIndex == 0)
                {
                    txtstatus.Text = "OK";
                }
                if (combStatus.SelectedIndex == 1)
                {
                    txtstatus.Text = "Damaged";
                }
                if (combStatus.SelectedIndex == 2)
                {
                    txtstatus.Text = "Repair in Progress";
                }
                if (combStatus.SelectedIndex == 3)
                {
                    txtstatus.Text = "Repaired";
                }
                if (combStatus.SelectedIndex == 4)
                {
                    txtstatus.Text = "Loaner";
                }

                combStatus.Items.Add(txtstatus.Text);



                try
                {
                    string Query = "update rmsmasterdbtest.dbo.serial set serialnumber='" + this.txtsn.Text + "' ,serialnumber3='" + this.txtstatus.Text + "' where serialnumber='" + this.txtsn.Text + "' ;";
                    SqlCommand cmd = new SqlCommand(Query, Myconn);
                    SqlDataReader Reader;


                    Myconn.Open();
                    Reader = cmd.ExecuteReader();
                    MessageBox.Show("Updated Status");



                    while (Reader.Read())
                    {

                    }




                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Myconn.Close();
            }

Open in new window

After I wrote your code it asking me for catch " expected catch or finally"
ASKER CERTIFIED SOLUTION
Avatar of jorge_toriz
jorge_toriz
Flag of Mexico image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much now it works.

I have another small issue when I select some message from the dropbox and and click update it is update the database and duplicating the combobox item
Are you working on a windows application or web application?

I need to know it because the ComboBox is different from DropDownList.
windows form application
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great help solved my problem