如何使用combobox id显示名称 [英] How to Display name using combobox id

查看:291
本文介绍了如何使用combobox id显示名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人请帮助



我有2张表格。在第一个表单中有两个文本框ID和名称和一个保存按钮,第二个表单有一个组合和一个文本框。我想,当加载form-2然后数据库表id填写表格-2 combobox并且如果我在combobox中选择任何id然后在表单2文本框中释放id名称显示。



i尝试了这个...但是一个错误即将来临

错误 - >将varchar值'System.Data.DataRowView'转换为数据类型时转换失败

 // FORM-1 CODE 
private void cmdsave_Click(object sender,EventArgs e)
{
conn = new SqlConnection(@Data Source = MANISH-PC\SQLEXPRESS ; Initial Catalog = test2.mdf; Integrated Security = True);
conn.Open();
SqlCommand command = new SqlCommand();
command.CommandText =插入table1(名称)值('+ textBox2.Text +');
command.Connection = conn;
command.ExecuteNonQuery();
MessageBox.Show(已成功保存);
}

// FORM-2 CODE

private void Form2_Load(object sender,EventArgs e)
{
// TODO:这行代码将数据加载到'_test2_mdfDataSet.Table1'表中。您可以根据需要移动或删除它。
this.table1TableAdapter.Fill(this._test2_mdfDataSet.Table1);
}

private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
SqlConnection conn = new SqlConnection(@Data Source = MANISH-PC\SQLEXPRESS; Initial Catalog = test2.mdf; Integrated Security = True);
conn.Open();
SqlCommand command = new SqlCommand(select table from table1 where id ='+ comboBox1.SelectedItem +',conn);
SqlDataAdapter adp = new SqlDataAdapter(command);
tbl = new DataTable();
adp.Fill(tbl);
textBox1.Text = tbl.Rows [1] .ToString();
}

解决方案

那是因为你要分配 comboBox1.SelectedItem 查询中的id。这实际上是一个对象,但你应该提供价值。



使用 SelectedValue Property [ ^ ]获取值。

 SqlCommand command =  new  SqlCommand( 从table1中选择名称,其中id =' + comboBox1。 SelectedValue  +  ,conn); 



但是你应该总是使用参数化查询来避免 SQL注入问题

 SqlCommand command =  new  SqlCommand( 从选项卡中选择名称le1,其中id = @ id,conn); 
command.Parameters.AddWithValue( @ id,comboBox1.SelectedValue);


Anyone Please help

i have 2 form . in first form have two textbox id and name and one save button and second form have one combo and one textbox. i want, when form-2 loaded then database table id's fill in form -2 combobox and if i select any id in combobox then releted id name display in form 2 textbox.

i tried this...but one error is coming
ERROR->Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type

 // FORM-1 CODE
 private void cmdsave_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(@"Data Source=MANISH-PC\SQLEXPRESS;Initial Catalog=test2.mdf;Integrated Security=True");
            conn.Open();
            SqlCommand command = new SqlCommand();
            command.CommandText = "insert into table1(name)values('" + textBox2.Text + "')";
            command.Connection = conn;
            command.ExecuteNonQuery();
            MessageBox.Show("successfully Saved");
         }
 
//FORM-2 CODE

 private void Form2_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the '_test2_mdfDataSet.Table1' table. You can move, or remove it, as needed.
            this.table1TableAdapter.Fill(this._test2_mdfDataSet.Table1);
             }
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=MANISH-PC\SQLEXPRESS;Initial Catalog=test2.mdf;Integrated Security=True");
            conn.Open();
            SqlCommand command = new SqlCommand("select name from table1 where id='"+comboBox1.SelectedItem+"'", conn);
            SqlDataAdapter adp = new SqlDataAdapter(command);
            tbl = new DataTable();
            adp.Fill(tbl);
            textBox1.Text = tbl.Rows[1].ToString();
}

解决方案

That is because you are assigning comboBox1.SelectedItem to id in query. That is actually a object, but you should be providing the value.

Use SelectedValue Property[^] to get the value.

SqlCommand command = new SqlCommand("select name from table1 where id='" + comboBox1.SelectedValue +"'", conn);


But you should always use Parameterized query to avoid SQL Injection issues.

SqlCommand command = new SqlCommand("select name from table1 where id=@id", conn);
command.Parameters.AddWithValue("@id", comboBox1.SelectedValue);


这篇关于如何使用combobox id显示名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆