c# - 如何避免在 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();
}
}
解决方案
你的sql是:
select id,name,gen,age,date,cont
这意味着,就列编号而言,0 是 id,1 是名称......
你说:
它应该将患者姓名存储在文本框 2 中
你写道:
textBox2.Text = dr.GetValue(0).ToString();
0 是标识。
也许您应该切换到名称,包括为您的控件提供合理的名称。看看它使理解代码变得多么容易:
nameTextBox.Text = dr["name"].ToString();
推荐阅读
- reactjs - JSS在哪里写动态MaterialUI类?
- react-native - React Native 如何在不转到该屏幕的情况下将道具传递到另一个屏幕
- gensim - Mallet 中的一致性和诊断文件
- java - UDP 服务器无法正确发回音频
- c++ - 如何用随机值填充数组
- kotlin - 带有 catch 运算符的 Kotlin Flow 仍然完成
- python - 我的 colab 笔记本崩溃了,我该如何解决?
- php - 成功提交后隐藏登录表单
- google-apps-script - 我不知道为什么这个脚本忽略了向某些收件人发送电子邮件
- laravel - 卡到尽头怎么回线?