首页 > 解决方案 > 如何避免在 Windows 窗体应用程序的文本框中存储重复的日期?

问题描述

下面的代码将患者 ID 存储在文本框 1 和文本框 2 中。它应该将患者姓名存储在文本框 2 中。我尝试更改 dr.GetValue(1).ToString() 中的值,但事实并非如此在职的!有人可以帮忙吗?

 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();

                        //      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();
            }
        }

输出

标签: c#winforms

解决方案


你的sql是:

select id,name,gen,age,date,cont

这意味着,就列编号而言,0 是 id,1 是名称......

你说:

它应该将患者姓名存储在文本框 2 中

你写道:

textBox2.Text = dr.GetValue(0).ToString();

0 是标识。

也许您应该切换到名称,包括您的控件提供合理的名称。看看它使理解代码变得多么容易:

nameTextBox.Text = dr["name"].ToString();

推荐阅读