在文本框中存储错误的数据!

编纂

会员
已加入
2020年12月4日
留言内容
18
编程经验
1-3
你好!在窗口形式中,患者ID同时存储在文本框1和文本框2中。它不在textbox2中存储患者的姓名。请帮忙!
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();
            }
        }
 

附件

  • 1611472151307.png
    1611472151307.png
    23.6 KB · 观看次数: 1

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,498
地点
悉尼,澳大利亚
编程经验
10+
For future reference, there's no way that a sensible question can be posted in the SQL服务器 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服务器 and putting data 进入 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.
 
最佳 底部