Storring wrong data in a text box!

codify

Member
Joined
Dec 4, 2020
Messages
18
Programming Experience
1-3
Hello! In a window form, Patient id is being stored in both text box1 and textbox2. It is not storing the name of the patient in textbox2. Please help!
C#:
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A85V0ME\SQLEXPRESS;Initial Catalog=Hospitalmanagement;Integrated Security=True");

            con.Open();
            if (textBox1.Text != "")
            {
                try
                {
                    string getCust = "select id,name,gen,age,date,cont,addr,disease,status,r_type,building,r_no,price from patient where id=" + Convert.ToInt32(textBox1.Text) + " ;";

                    SqlCommand cmd = new SqlCommand(getCust, con);
                    SqlDataReader dr;
                    dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        textBox2.Text = dr.GetValue(0).ToString();
                        if (dr[1].ToString() == "Male")
                        {
                            radioButton1.Checked = true;
                        }
                        else if (dr[1].ToString() == "Female")
                        {
                            radioButton2.Checked = true;
                        }

                        textBox3.Text = dr.GetValue(1).ToString();
                        textBox3.Text = dr.GetValue(3).ToString();
                        textBox4.Text = dr.GetValue(4).ToString();
                        textBox5.Text = dr.GetValue(5).ToString();
                        textBox6.Text = dr.GetValue(6).ToString();
                        textBox7.Text = dr.GetValue(7).ToString();
                        textBox8.Text = dr.GetValue(8).ToString();
                        textBox9.Text = dr.GetValue(10).ToString();
                        textBox10.Text = dr.GetValue(9).ToString();
                        textBox11.Text = dr.GetValue(11).ToString();
                        textBox12.Text = dr.GetValue(12).ToString();
                
                      
                    }
                    else
                    {
                        MessageBox.Show(" Sorry, This ID, " + textBox1.Text + " Staff is not Available.   ");
                        textBox1.Text = "";
                    }
                }
                catch (SqlException excep)

                {
                    MessageBox.Show(excep.Message);
                }
                con.Close();
            }
        }
 

Attachments

  • 1611472151307.png
    1611472151307.png
    23.6 KB · Views: 22
look at line 27
 
Solution
For future reference, there's no way that a sensible question can be posted in the SQL Server forum and then be titled relating to a TextBox. This is a perfect example of one of the main reasons why beginners have trouble solving problems. They don't take enough time to work out where in the process the problem is. Getting data from SQL Server and putting data into a TextBox are two completely separate and unrelated things. Either the correct data comes out of the database or not. If it doesn't then the issue has nothing to do with a TextBox and if it does then the issue has nothing to do with the database. Think the problem through, break the problem down and debug your code to narrow down the cause as much as you possibly can first. If you had done that then you'd have seen where the problem was. In fact, you really didn't even need to do that because if you had simply read the line that was supposed to populate that TextBox, you'd have seen that it wasn't. You need to be a lot more systematic when it comes to diagnosing issues. It's all part of the learning experience.
 
Back
Top Bottom