如何使用数据集从存储过程填充下拉列表 [英] how to populate a dropdown list from stored procedure using dataset

查看:94
本文介绍了如何使用数据集从存储过程填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我对此并不陌生,所以我希望有人能指出我在教程或代码示例的方向.我在网上看到了很多东西,其中的特定部分是在VB中完成的.我将存储过程放在SQL&表中.我正在尝试使用数据集从存储过程的记录中填充一个asp下拉列表.有人知道如何在C#中执行此操作吗?

我正在使用3层Architechure

我希望你们能帮助我.
谢谢

我是

Hey everyone!

I am new to this, so I was hoping if someone could point me in the direction of a tutorial or a code sample. I have seen a bunch of stuff on the web, with this particular piece done in VB. I have my stored procedures in SQL & tables in place. I am trying to populate an asp dropdownlist from the records of a stored procedure using dataset. Does anyone know how to do this in C#?

I am using 3 layer architechure

I hope u guys can help me out.
Thanks

I am

推荐答案



请尝试以下代码.

dropdownlist的aspx页面代码
Hi,

Try with the below code.

aspx page code for dropdownlist
<asp:dropdownlist id="ddlName" runat="server" datatextfield="<%# Eval("TextFieldColumnName") %>" datavaluefield="<%# Eval("ValueFieldColumnName")%>" xmlns:asp="#unknown">
       
        </asp:dropdownlist>



背后的代码



Code Behind

public DataSet GetItems()
{
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("procedureName;", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    try
    {
        con.Open();
        da.Fill(ds);
        con.Close();       
    }
    catch (Exception ex)
    {
        //write error message
    }  
     return ds; 
}



绑定下拉列表



bind Dropdown

ddlName.DataSource = GetItems();
ddlName.DataBind();


这不是解决您问题的确切方法,但我希望这会帮助您获得一些解决问题的想法.

这是我用于此样本的SP

This is not the exact solution for your problem but I hope this will help you to get some idea to solve your problem.

This is the SP I used for this sample

CREATE proc [dbo].[ReadAllImageIDs] as 
SELECT ImageID FROM ImageData 
GO



以下是loadImageIDs方法



below is the loadImageIDs method

private void loadImageIDs()
{
    #region Load Image Ids
    SqlConnection con = new SqlConnection(DBHandler.GetConnectionString());
    SqlCommand cmd = new SqlCommand("ReadAllImageIDs", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    try
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            cmbImageID.DataSource = dt;
            cmbImageID.ValueMember = "ImageID";
            cmbImageID.DisplayMember = "ImageID";
            cmbImageID.SelectedIndex = 0;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        if (con.State == ConnectionState.Open)
            con.Close();
    }
    #endregion
}



了解更多详情
使用Streded Procedures和C#.net从SQL Server存储和检索图像 [ ^ ]

谢谢



for more details
Storing and Retrieving Images from SQL Server Using Strored Procedures and C#.net[^]

Thanks


这篇关于如何使用数据集从存储过程填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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