我试过,但它没有在组合框中工作数据没有从数据库中检索 [英] i tried but it is not working in combo box data is not retrieved from the database

查看:93
本文介绍了我试过,但它没有在组合框中工作数据没有从数据库中检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下



在pageload中我写下面的代码如下



My code as follows

In pageload i written the below code as follows

SqlConnection conn = new SqlConnection(@"Data Source=DOCTOR\SQLEXPRESS;Initial Catalog=Testing;User ID=sa;Password=a");

conn.Open();
SqlCommand sc = new SqlCommand("select Designation from Designations where active <> 'd'", conn);
 SqlDataReader reader;
 reader = sc.ExecuteReader();
 DataTable dt = new DataTable();
 dt.Columns.Add("Designation", typeof(string));
 dt.Load(reader);
 dt.Load(reader);
 ComboBox1.DataTextField = "Designation";
 ComboBox1.DataValueField = "Designation";
 ComboBox1.DataSource = dt;
 ComboBox1.DataBind();
 conn.Close();





在运行模式下如下

指定组合框



但是在组合框中没有显示。我尝试了好几次但是在组合框中没有显示。



请帮我解决上面代码中的问题。



In run mode as follows
Designation Combobox

But in combobox designation is not displayed. I tried several time but in combobox designation is not shown.

Please help me what is the problem in my above code.

推荐答案

有不少可能存在的问题,例如:



  • 查询条件导致无法返回行
  • 表格为空
  • ComboBox1实际上并不是正确的对象,依此类推
There are quite a few possible problems, like:

  • The condition of the query causes no rows to be returned
  • The table is empty
  • The ComboBox1 isn't actually the correct object and so on


  • 我没有使用阅读器并加载数据表,而是使用SqlDataAdapter,定义选择命令 [ ^ ]并使用 SqlDataAdapter.Fill [ ^ ]填充数据表。
  • 我建议将对象重命名为有意义的东西,而不是像ComboBox1这样的默认名称。这有助于理解代码并发现错误。

  • Instead of using the reader and loading the data table, I'd use SqlDataAdapter, define the SelectCommand[^] and use the SqlDataAdapter.Fill[^] to populate the data table.
  • I suggest renaming the objects to something meaningful instead of the default names like ComboBox1. This helps to understand the code and also to spot errors.


阅读掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]并尝试调试应用程序并检查在运行时获得的值以及哪些对象不按预期行事。

您可以使用 SqlDataAdapter 填充Datatable [ ^ ]并将数据表设置为DataSource,示例代码:

Read Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] and try to debug your application and check the values you get at runtime and also which objects not behave as you expected.
you can fill Datatable using SqlDataAdapter[^] and set the datatable as DataSource, sample code:
using(SqlConnection con = new SqlConnection(ConnectionString))
using(SqlCommand cmd = new SqlCommand("select Designation from Designations where active <> 'd'", con))
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
     DataTable dt=new DataTable();
     da.Fill(dt);
     ComboBox1.DataTextField = "Designation";
     ComboBox1.DataValueField = "Designation";
     ComboBox1.DataSource = dt;
     ComboBox1.DataBind();
}



在sql management studio中运行sql语句并确保你有sql语句的数据.debug并检查上面的代码并检查数据表行。更新问题有任何例外细节。


run the sql statement in sql management studio and make sure you have data for the sql statement. debug and check above code and check the datatable rows. update the question with exception details if any.


这篇关于我试过,但它没有在组合框中工作数据没有从数据库中检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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