[英]Filling Textbox, when combobox Selected Index Changed
In the code below, i just want to fill my textbox based on the selected combo box changed. 在下面的代码中,我只想基于已更改的所选组合框来填充我的文本框。 but i get the following error. 但我得到以下错误。
'Conversion failed when converting the varchar value 'System.Data.DataRowViewConvert.ToString()' to data type int.' '将varchar值'System.Data.DataRowViewConvert.ToString()'转换为数据类型int时转换失败。'
i'd appreciate it if you help me. 如果您能帮助我,我将不胜感激。
SqlConnection objConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\UniversityDataBase.mdf;Integrated Security=True");
private void comboBox1_Click(object sender, EventArgs e)
{
string query = "SELECT *FROM TutorTable";
SqlDataAdapter SDA = new SqlDataAdapter(query, objConnection);
DataTable dt = new DataTable();
SDA.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Tid";
objConnection.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string sqlQuery = "SELECT *FROM TutorTable where Tid = '"+comboBox1.Text+ "Convert.ToString()'";
SqlCommand objCommand = new SqlCommand(sqlQuery, objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
SqlDataReader dr;
dr = objCommand.ExecuteReader();
while (dr.Read())
{
string Tname = (string)dr["Tname"].ToString();
textBox1.Text = Tname;
}
}
Why are you using the Convert.ToString() in this piece of code: 为什么在这段代码中使用Convert.ToString():
"SELECT * FROM TutorTable where Tid = '"+comboBox1.Text+ "Convert.ToString()'"
I think the correct way wolud be: 我认为正确的方法是:
"SELECT * FROM TutorTable where Tid = '"+comboBox1.Text+ "'"
But consider using a store procedure to prevent sql injection or using an ORM. 但是请考虑使用存储过程来防止sql注入或使用ORM。
I think that comboBox1.Text
is already returning a string value. 我认为comboBox1.Text
已经返回一个字符串值。 So if that, it is not necessary to put the Convert.ToString(comboBox.Text)
, just put comboBox1.Text 因此,如果这样,则不必放入Convert.ToString(comboBox.Text)
,只需放入comboBox1.Text
According to documentation, the Text property is a string https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.text(v=vs.110).aspx 根据文档,Text属性是字符串https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.text(v=vs.110).aspx
I found the Solution as follows: 我发现解决方案如下:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\UniversityDataBase.mdf;Integrated Security=True");
string sqlQuery = "SELECT *FROM TutorTable WHERE Tid = '" + comboBox1.Text + "'";
SqlCommand objCommand = new SqlCommand(sqlQuery, con);
con.Open();
SqlDataReader dr;
dr = objCommand.ExecuteReader();
while (dr.Read())
{
string name = (string)dr["Tname"].ToString();
textBox1.Text = name;
}
}
You have got the answer already but few more things should be taken care of 您已经有了答案,但应该多做一些事情
Your code 您的密码
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string sqlQuery = "SELECT *FROM TutorTable where Tid = '"+comboBox1.Text+ "Convert.ToString()'";
SqlCommand objCommand = new SqlCommand(sqlQuery, objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
SqlDataReader dr;
dr = objCommand.ExecuteReader();
while (dr.Read())
{
string Tname = (string)dr["Tname"].ToString();
textBox1.Text = Tname;
}
}
Things to notice: 注意事项:
1) Use varabled instead of manipulating sql eg SELECT *FROM TutorTable where Tid = @id
and pass id into sqlCommand object 1)使用varabled而不是操作sql,例如SELECT *FROM TutorTable where Tid = @id
并将id传递给sqlCommand对象
2) You dont need to call ExecuteNonQuery
before SqlDataReader
2)您不需要在SqlDataReader
之前调用ExecuteNonQuery
3) you need to use if(dr.Read()
) instead of while
3)您需要使用if(dr.Read()
)代替while
4) You can directly assign value textbox. 4)您可以直接分配值文本框。 eg texBox1.Text = dr["Tname"].ToString();
例如texBox1.Text = dr["Tname"].ToString();
5) Close the objConnection
5)关闭objConnection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.