How to determe DataValueField and DataTextField for dropdownlist depending on the following query

Hi, please consider the following query:
SELECT '1', '2', '3'

Now, I am executing the following code to populate a dropdownlist but I'm confused about what should I set  DataTextField for dropdownlist?

The second question is how can I modify the sql statnment to add a text field for each value.

Thank you.
SqlCommand cmd = new SqlCommand("SELECT '1', '2', '3'", new SqlConnection(ConfigurationManager.ConnectionStrings["AssetTrackConnectionString"].ConnectionString));
            cmd.Connection.Open();

            SqlDataReader ddlValues;
            ddlValues = cmd.ExecuteReader();

            drpdwnlst.DataSource = ddlValues;
            drpdwnlst.DataValueField = ?;
            drpdwnlst.DataTextField = ?;
            drpdwnlst.DataBind();

            cmd.Connection.Close();
            cmd.Connection.Dispose();

Open in new window

LVL 3
Abdu_AllahAsked:
Who is Participating?
 
rajeeshmcaCommented:
Instead of ExecuteReader, try with the executeNonQuery() and fill  the DataTable with the Data from the query. Then assign the dataText and DataValue field accordingly like..

SqlCommand cmd = new SqlCommand("SELECT '1', '2', '3'", new SqlConnection(ConfigurationManager.ConnectionStrings["AssetTrackConnectionString"].ConnectionString));
            cmd.Connection.Open();
SqlDataAdapter objSqlAdp = new SqlDataAdapter(cmd);
DataTable objDT = new DataTable();
objSqlAdp.Fill(objDT);

drpdwnlst.DataSource = objDT ;
drpdwnlst.DataValueField = objDT[0];
drpdwnlst.DataTextField = objDT[1];
drpdwnlst.DataBind();



0
 
rajeeshmcaCommented:
Hi Abdu_allah,

U should give a column name, otherwise it wont be possible for u to set the Value and textfield.

The other thing u can try it out is, like specifying the Index of the column in the Datatable.


The one u want to display for the users u give that as DataTextField and the otherOne which u can use to do some validations can be used as DataValueField like

drpdwnlst.DataSource = ddlValues;
            drpdwnlst.DataValueField = "1";
            drpdwnlst.DataTextField = "2";
            drpdwnlst.DataBind();
0
 
Abdu_AllahAuthor Commented:
how can I give a column name?
 
>drpdwnlst.DataValueField = "1";

No, I need to do that throught a SQL statement.(the sql will NOT retrieve values from tables it will have its data)
 
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Abdu_AllahAuthor Commented:
Got compilation error!
0
 
rajeeshmcaCommented:
where did you get the error and what is that saying
0
 
rajeeshmcaCommented:
Hi try this

string conn = ConfigurationManager.ConnectionStrings["AssetTrackConnectionString"].ConnectionString;

        SqlConnection objConn = new SqlConnection(conn );
        SqlCommand cmd = new SqlCommand("SELECT '1', '2', '3'", objConn);
        cmd.Connection.Open();
        SqlDataAdapter objSqlAdp = new SqlDataAdapter(cmd);
        DataTable objDT = new DataTable();
        objSqlAdp.Fill(objDT);

        TestDdl.DataSource = objDT;
        TestDdl.DataValueField = objDT.Columns[0].ToString();
        TestDdl.DataTextField = objDT.Columns[1].ToString();
        TestDdl.DataBind();
0
 
amar31282Commented:
can you tell you want to come into the dropdownlist....

it seems strance

 SELECT '1', '2', '3'

because it will always return 3 columns*1 row

What you want to do exactly
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.