Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

c# - Storing duplicate date in text box in windows form application

The below code is storring patients id in both text box 1 and text box 2. It should store the name of patient in the textbox 2. I have tried changing the values in dr.GetValue(1).ToString(), but its not working! Can anyone please help?

 private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A85V0MESQLEXPRESS;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();
            }
        }

Output

question from:https://stackoverflow.com/questions/65868196/storing-duplicate-date-in-text-box-in-windows-form-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your sql is:

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

Which means, in terms of column numbering, that 0 is id, 1 is name...

You said:

It should store the name of patient in the textbox 2

And you wrote:

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

0 is the id.

Perhaps you should switch to names, for everything including giving your controls sensible names. Look how much easier it makes it to understand the code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...