Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databind a dropdownlist

When I try to databing dropdownlist, got this: system.data.datarowview
what do I wrong?

 string strQuery = "Select Item FROM Calendar Where UserD="Test";
 SqlConnection myConn;
 SqlDataAdapter sqlDa = new SqlDataAdapter(strQuery,myConn);
 DataTable sqlTa = new DataTable("Test");
 da.Fill(sqlTa);
 ddlList.DataSource = sqlTa;
 ddlList.DataBind();
like image 401
Will_G Avatar asked Mar 08 '13 11:03

Will_G


1 Answers

string strQuery = "Select Item FROM Calendar Where UserD='Test'";

Note you need to use single quotations around the string, as in your code you didn't finish the inital string ever, so the rest of the code just became part of strQuery.

In addition if you bring back more than one field in the future, when you bind a dropdown list you need to specify which field from the database is the value and which is the displayed text.

ddlList.DataSource = sqlTa;
ddlList.DataValueField = "ValueFieldFromDatabaseResults";
ddlList.DataTextField = "ShownTextFieldFromDatabaseResults";
ddlList.DataBind();
like image 133
Ryan McDonough Avatar answered Sep 22 '22 02:09

Ryan McDonough