Solved

C# code

Posted on 2014-04-11
10
327 Views
Last Modified: 2014-04-11
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
0
Comment
Question by:Moti Mashiah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 39994655
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.
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 39994731
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

0
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 39994956
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

0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 1

Author Comment

by:Moti Mashiah
ID: 39995180
After I wrote your code it asking me for catch " expected catch or finally"
0
 
LVL 15

Accepted Solution

by:
jorge_toriz earned 500 total points
ID: 39995205
Ups, sorry, I wrote the code on Notepad and I didn't notice that, here is the fixed code:
//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);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Myconn.Close();
            } 

Open in new window

0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 39995234
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
0
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 39995283
Are you working on a windows application or web application?

I need to know it because the ComboBox is different from DropDownList.
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 39995293
windows form application
0
 
LVL 15

Assisted Solution

by:jorge_toriz
jorge_toriz earned 500 total points
ID: 39995438
Instead of:
combStatus.Items.Add(txtstatus.Text);

Open in new window


Put:
if (combStatus.Items.IndexOf(txtstatus.Text) == -1)
    combStatus.Items.Add(txtstatus.Text);

Open in new window

0
 
LVL 1

Author Closing Comment

by:Moti Mashiah
ID: 39995462
Great help solved my problem
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question