?
Solved

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

Posted on 2009-12-29
7
Medium Priority
?
341 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:Abdu_Allah
  • 4
  • 2
7 Comments
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26138990
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
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 26140458
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
 
LVL 15

Accepted Solution

by:
rajeeshmca earned 2000 total points
ID: 26143324
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Abdu_Allah
ID: 26143755
Got compilation error!
0
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26143795
where did you get the error and what is that saying
0
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26143820
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
 
LVL 14

Expert Comment

by:amar31282
ID: 26146176
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question