Dropdownlist and Datareader

Posted by salvationishere on Stack Overflow See other posts from Stack Overflow or by salvationishere
Published on 2010-04-08T23:25:06Z Indexed on 2010/04/08 23:33 UTC
Read the original article Hit count: 478

Filed under:
|
|
|

After trying many solutions listed on the internet I am very confused now. I have a C#/SQL web application for which I am simply trying to bind an ExecuteReader command to a Dropdownlist so the user can select a value. This is a VS2008 project on an XP OS.

How it works is after the user selects a table, I use this selection as an input parameter to a method from my Datamatch.aspx.cs file. Then this Datamatch.aspx.cs file calls a method from my ADONET.cs class file. Finally this method executes a SQL procedure to return the list of columns from that table. (These are all tables in Adventureworks DB). I know that this method returns successfully the list of columns if I execute this SP in SSMS. However, I'm not sure how to tell if it works in VS or not.

This should be simple. How can I do this? Here is some of my code. The T-sQL stored proc:

CREATE PROCEDURE [dbo].[getColumnNames]
@TableName VarChar(50) AS
BEGIN
SET NOCOUNT ON;
SELECT col.name 'COLUMN_NAME' FROM sysobjects obj
INNER JOIN syscolumns col ON obj.id = col.id
WHERE obj.name = @TableName
END

It gives me desired output when I execute following from SSMS: exec getColumnNames 'AddressType'

And the code from Datamatch.aspx.cs file currently is:

// Add DropDownList Control to Placeholder
    private void CreateDropDownLists()
    {
        SqlDataReader dr2 = ADONET_methods.DisplayTableColumns(targettable);

         int NumControls = targettable.Length;
         DropDownList ddl = new DropDownList();

    }

Where ADONET_methods.DisplayTableColumns(targettable) is:

public static SqlDataReader DisplayTableColumns(string tt)
    {
        SqlDataReader dr = null;
        string TableName = tt;
        string connString = "Server=(local);Database=AdventureWorks;Integrated Security = SSPI";
        string errorMsg;
        SqlConnection conn2 = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand("getColumnNames");  //conn2.CreateCommand();

        try
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn2;
            SqlParameter parm = new SqlParameter("@TableName", SqlDbType.VarChar);
            parm.Value = "Person." + TableName.Trim();
            parm.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(parm);
            conn2.Open();
            dr = cmd.ExecuteReader();

        }
        catch (Exception ex)
        {
            errorMsg = ex.Message;
        }
        return dr;
    }

© Stack Overflow or respective owner

Related posts about dropdownlist

Related posts about datareader