我想要做的是当用户从comboBox中选择公司然后单击按钮时,不同的文本框应该显示数据库中的不同数据。
这就是我所拥有的,但它似乎无法奏效。
有什么建议吗?
private void Edit_Load(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(str))
{
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter("SELECT * FROM Companies", con);
DataTable dt = new DataTable();
adapt.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Name";
}
}
private void button3_Click(object sender, EventArgs e)
{
String company = comboBox1.SelectedItem.ToString();
String check = @"SELECT * FROM Companies WHERE Name=@name";
using (SqlConnection con = new SqlConnection(str))
using (SqlCommand cmd = new SqlCommand(check, con))
{
con.Open();
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = company;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
textBox1.Text = (reader["Name"].ToString());
textBox2.Text = (reader["PhNo"].ToString());
textBox3.Text = (reader["Email"].ToString());
textBox4.Text = (reader["Acc"].ToString());
textBox5.Text = (reader["Address"].ToString());
textBox6.Text = (reader["Suburb"].ToString());
textBox7.Text = (reader["PostCode"].ToString());
textBox8.Text = (reader["State"].ToString());
}
}
}
更新:comboBox1.SelectedItem.ToString();
的输出为System.Data.DataRowView
,因此似乎没有注册所选项目。我该如何解决这个问题?
答案 0 :(得分:2)
当您的组合框的DataSource
为DataTable
时,SelectedItem
中的对象属于DataRowView
类型。
因此,要获取字段,您可以将所选项目转换为DataRowView
并以这种方式提取字段值:
var name = ((DataRowView)comboBox1.SelectedItem)["Name"].ToString();
实际上((DataRowView)comboBox1.SelectedItem)["FieldName"]
的类型为object
,您应该将该字段转换为所需类型。
答案 1 :(得分:0)
试试这个
String check = "SELECT * FROM Companies WHERE Name=@name";
using (SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=ABCD;Integrated Security=True"))
using (SqlCommand cmd = new SqlCommand(check, con))
{
con.Open();
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = comboBox1.SelectedItem.ToString();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
textBox1.Text = reader["Name"].ToString();
textBox2.Text = reader["PhNo"].ToString();
textBox3.Text = reader["Email"].ToString();
textBox4.Text = reader["Acc"].ToString();
textBox5.Text = reader["Address"].ToString();
textBox6.Text = reader["Suburb"].ToString();
textBox7.Text = reader["PostCode"].ToString();
textBox8.Text = reader["State"].ToString();
}
}
确保您的连接字符串正确。你有没有检查过comboBox1.SelectedItem.ToString()的值,db中是否存在同样的值?
如果您正在填充数据库中的下拉列表,请参阅:System.Data.DataRowView in DropDownList
更新:
private void ComboBoxBinding()
{
using (SqlConnection con = new SqlConnection(str))
{
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter("SELECT Name,Id FROM Companies", con);
DataTable dt = new DataTable();
adapt.Fill(dt);
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = dt;
comboBox1.DataBind();
}
}
您可以在页面加载或类构造函数中调用此函数。
答案 2 :(得分:0)
尝试下面的代码它会起作用,你缺少的是你没有设置组合框的ValueMember属性。
建议: 1.调试代码并确保查询正确,并且能够从数据源获取正确的值。 2.确保数据库表中的列名与组合框显示成员和值成员中的列名完全相同
using (SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=\"Student Extended\";Integrated Security=True"))
{
SqlCommand command = new SqlCommand
{
Connection = connection,
CommandText = "SELECT DepartmentId,DepartmentName FROM dbo.TblDepartment"
};
SqlDataAdapter adpater = new SqlDataAdapter(command);
DataTable table = new DataTable();
adpater.Fill(table);
if (txtStdDeptName != null)
{
txtStdDeptName.DataSource = table;
txtStdDeptName.DisplayMember = "DepartmentName";
txtStdDeptName.ValueMember = "DepartmentId";
}
}