Can't Populate the dropdown list . Can anyone help , possibly some tutorials on my mistakes? I don't understand exactly how to populate my datasource. Pretty new to C# or ASP.Net so go easy on me.
protected void Button1_Click(object sender, EventArgs e)
{
List<string> _newList = new List<string>();
if (TextBox1.Text != null)
{
OdbcDataReader MapResult; //Data Reader
Database db = new Database();
string lat = "";
string sql = " SELECT
informix.dbimg_mstr_rec.doc_path
FROM
informix.dbimg_mstr_rec
WHERE
informix.dbimg_mstr_rec.doc_key
= '" + TextBox1.Text + "'";
try
{
MapResult = db.ExecQuery(sql, timeOut);
if (MapResult.Read())
{
lat = MapResult["doc_path"].ToString();
DropDown1.DataSource = _newList;
DropDown1.DataTextField = "doc_path";
DropDown1.DataValueField = "doc_path";
form1.Controls.Add(DropDown1);
} //end if
MapResult.Close();
db.CloseConnection();
//end while
}//end try
catch (OdbcException ex)
{
errList.Add("[User] Error 109: " + ex.Message);
} //end catch
return;
} //end if
else
return;
}
It doesn't look like you're actually binding the dropdown anywhere in this code. You need to do something like this:
DropDown1.DataTextField = "doc_path";
DropDown1.DataValueField = "doc_path";
DropDown1.DataSource = dr.ExecuteReader();
DropDown1.DataBind();
Alternately, you can do this all in your page markup by using a SqlDataSource control.
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataSet"
ConnectionString="myConnString"
SelectCommand="myStoredProcedure"
>
</asp:SqlDataSource>
<asp:MyDropDownList id="DropDown1" runat="server" DataSource="SqlDataSource1"
DataTextField="COLUMN_NAME" DataValueField="COLUMN_NAME" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.