使用C#从SQL表填充列表框时出错 [英] Error in Populating a listbox from SQL table using C#

查看:75
本文介绍了使用C#从SQL表填充列表框时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

私有无效AutoradioButton1_Click(对象发送者,EventArgs e)
{
SqlConnection conn =新的SqlConnection("Server = CHRIS-PC \\ SQLEXPRESS; Database = jasmineSql.sdf;" +
集成安全性= True");
conn.Open();
DataSet sds = new DataSet();
SqlCommand scmd =新的SqlCommand("SELECT Description FROM TblCategories",conn);
SqlDataAdapter sadptr =新的SqlDataAdapter(scmd);
sadptr.Fill(sds,"TblCategories");
this.listBox1.DataSource = sds.Tables ["TblCategories"];
this.listBox1.DisplayMember =描述";
listBox1.DataBind();
}


从特定表的所需列填充列表框(我的数据库中有3个表)

请你能告诉我怎么回事.单击单选按钮时,我无法在listBox1的"TblCategories"列中看到输入的值.

错误-System.Windows.Forms.ListBox不包含
的定义 DataBind.

仍然无法在列表框中进行描述

注意:我正在获取ListBox1.Databindings();.不是listBox1.DataBind();
这是造成人口不足的原因吗?请帮忙.

谁能为我提供解决方案?
谢谢.

private void AutoradioButton1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Server=CHRIS-PC\\SQLEXPRESS;Database=jasmineSql.sdf;" +
"Integrated Security=True");
conn.Open();
DataSet sds = new DataSet();
SqlCommand scmd = new SqlCommand("SELECT Description FROM TblCategories", conn);
SqlDataAdapter sadptr = new SqlDataAdapter(scmd);
sadptr.Fill(sds, "TblCategories");
this.listBox1.DataSource = sds.Tables["TblCategories"];
this.listBox1.DisplayMember = "Description";
listBox1.DataBind();
}


Populating a Listbox from the desired column in a Particular table (I have 3 table in a my database)

Please can you tell me what is going worng. I coudn''t see the Values entered in the column "TblCategories" in my listBox1 when I clicked the radio button.

Error - System.Windows.Forms.ListBox does not contain a definition for
DataBind.

still unable to pull describtion in listbox

Note: I am getting ListBox1.Databindings(); Not listBox1.DataBind();
Is this causing to not populate. Please help.

Can anyone provide me with a solution?
Thanks.

推荐答案

您是否正在使用System.Windows.Forms.ListBox.

如果您在Windoows应用程序中,则无需使用DataBind.如果设置DataSource和DispayMember,那就没问题了.

如果省略行ListBox1.DataBind,您会遇到什么问题?
Are you using System.Windows.Forms.ListBox.

If you are in Windoows application you do not need to use DataBind. If you set DataSource and DispayMember it would be fine.

What problem do you face if you omit the line ListBox1.DataBind?


问题已解决.谢谢大家的帮助.

用所需的SQL colmn填充列表框:

正确的代码:

命名空间MovieProject
{
公共局部类Form1:Form
{
公共Form1()
{
InitializeComponent();
}

私有void AutoradioButton1_Click(对象发送者,EventArgs e)
{

SqlConnection conn =新的SqlConnection("Server = CHRIS-PC \\ SQLEXPRESS; Database = Tblcat;" +
集成安全性= True");


SqlCommand scmd =新的SqlCommand("SELECT Description FROM Moviecat",conn);

DataSet sds = new DataSet();


conn.Open();

SqlDataAdapter sadptr =新的SqlDataAdapter(scmd);
sadptr.Fill(sds,"Moviecat");


如果(sds.Tables ["Moviecat"].Rows.Count == 0)

{

MessageBox.Show(没有数据库.");

}

其他
{

this.listBox1.DataSource = sds.Tables ["Moviecat"];
this.listBox1.DisplayMember =描述";

}

conn.Close();

}


}
}
Problem solved. Thank you guys for help me.

Populating listbox with desired SQL colmn:

Correct code:

namespace MovieProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void AutoradioButton1_Click(object sender, EventArgs e)
{

SqlConnection conn = new SqlConnection("Server=CHRIS-PC\\SQLEXPRESS;Database=Tblcat;" +
"Integrated Security=True");


SqlCommand scmd = new SqlCommand("SELECT Description FROM Moviecat", conn);

DataSet sds = new DataSet();


conn.Open();

SqlDataAdapter sadptr = new SqlDataAdapter(scmd);
sadptr.Fill(sds, "Moviecat");


if (sds.Tables["Moviecat"].Rows.Count == 0)

{

MessageBox.Show("There are no DB.");

}

else
{

this.listBox1.DataSource = sds.Tables["Moviecat"];
this.listBox1.DisplayMember = "Description";

}

conn.Close();

}


}
}
<pre><pre>


这篇关于使用C#从SQL表填充列表框时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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