从数据库向列表框添加值 [英] Add values to List Box from Database

查看:79
本文介绍了从数据库向列表框添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下存储过程检索 CourseId,Course_name。

我想将这些值添加到列表框




以下是我的代码



The Below stored procedure retrieves CourseId, Course_name.
and i want to add these values into List Box.


Below is my code

public List<string> GetCourses()
        {
            string ConnectionString = connection();
            SqlConnection con = new SqlConnection(ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@Branch_code", "ap");
            cmd.CommandText = "sp_get_courses";
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;

            DataSet ds = new DataSet();
            da.Fill(ds);
            
            List<string> courses = new List<string>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

            {

             courses.Add(ds.Tables[0].Rows[i][1].ToString());

             courses.Add(ds.Tables[0].Rows[i][0].ToString());  

            }

             return courses;

        }

推荐答案

试试这样..



try like this..

protected void Page_Load(object sender, EventArgs e)
       {

           ListBox1.DataTextField = "CouseName";
           ListBox1.DataValueField = "CourseID";
           ListBox1.DataSource = GetCourses();
           ListBox1.DataBind();
       }


       public class Course
       {
           public string CourseID { get; set; }
           public string CouseName { get; set; }
       }

       public List<course> GetCourses()
       {
           string ConnectionString = connection();
           SqlConnection con = new SqlConnection(ConnectionString);
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.Parameters.AddWithValue("@Branch_code", "ap");
           cmd.CommandText = "sp_get_courses";
           cmd.CommandType = CommandType.StoredProcedure;

           SqlDataAdapter da = new SqlDataAdapter();
           da.SelectCommand = cmd;

           DataSet ds = new DataSet();
           da.Fill(ds);

           List<course> lstCourses = new List<course>();
           for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
           {
              string courseID =  ds.Tables[0].Rows[i][1].ToString();
              string courseName = ds.Tables[0].Rows[i][0].ToString();
              lstCourses.Add(new Course() { CourseID = courseID, CouseName = courseName });

           }
           return lstCourses;
       }</course></course></course>


什么不能正常工作?

以上将在每个CourseID的字符串列表中创建一个新字符串< b>& Course_Name。将datasetdirectly绑定到您正在使用的Listbox并设置列表框DataTextField和DataValuefield会不会更容易吗?
What is not working exactly?
The above will create a new string in the stringlist for each of CourseID & Course_Name. Would it not be easier to bind the datasetdirectly to the Listbox you are using, and set the listboxes DataTextField and DataValuefield?


对我而言,这样做会更容易,也更少工作有一个数据集。

To me it would be easier and less work to do this as you already have a dataset.
protected void Page_Load(object sender, EventArgs e)
       {

           ListBox1.DataTextField = "Course_Name";
           ListBox1.DataValueField = "CourseId";
           ListBox1.DataSource = GetCourses();
           ListBox1.DataBind();
       }


       public Dataset GetCourses()
       {
           string ConnectionString = connection();
           SqlConnection con = new SqlConnection(ConnectionString);
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.Parameters.AddWithValue("@Branch_code", "ap");
           cmd.CommandText = "sp_get_courses";
           cmd.CommandType = CommandType.StoredProcedure;

           SqlDataAdapter da = new SqlDataAdapter();
           da.SelectCommand = cmd;

           DataSet ds = new DataSet();
           da.Fill(ds);

           return ds;
       }


这篇关于从数据库向列表框添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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