I'm new to ASP.net and I'm trying to populate a dropdown list with values from a local SQL Database in Visual Studio.
This is the code I have but Its not working, could anyone assist?
{
SqlConnection PopulateListCon = new SqlConnection(ConnectionString);
try
{
if (PopulateListCon.State == ConnectionState.Closed)
PopulateListCon.Open();
String query = "SELECT * FROM ModuleTable WHERE UserId=@User AND ModuleSemester=@Sem";
SqlCommand sqlCmd = new SqlCommand(query, PopulateListCon);
sqlCmd.Parameters.Add("@User", SqlDbType.VarChar);
sqlCmd.Parameters["@User"].Value = userIdentification;
sqlCmd.Parameters.Add("@Sem", SqlDbType.VarChar);
sqlCmd.Parameters["@Sem"].Value = semester;
SqlDataReader dr1 = sqlCmd.ExecuteReader();
while (dr1.Read())
{
string modName = dr1.GetString(3);
Ddl_Module_Info_Time_Allocation_Module_Code.Items.Add(modName);
}
}
catch (Exception ex)
{
errMsg = ex.Message;
Response.Write("<script>alert('Error: " + errMsg + "')</script>");
}
finally
{
PopulateListCon.Close();
}
}
this is the Code for the Drop Down List:
<asp:DropDownList ID="Ddl_Module_Info_Time_Allocation_Module_Code" runat="server" style="z-index: 3; left: 330px; top: 10px; position: absolute" Height="24px" Width="128px" Visible="False"></asp:DropDownList>
If anyone could assist it would be appreciated