<tr>
<td colspan="2" style="height: 32px" align="left">
Select Branch
<asp:DropDownList ID="ddlBranches" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" CausesValidation="True">
<asp:ListItem Value="-1">---Select---</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="ddlBranches" CssClass="red_text" Display="Dynamic"
ErrorMessage="Please enter Branches" InitialValue="-1" SetFocusOnError="True"
ValidationGroup="Eventsp"></asp:RequiredFieldValidator>
</td>
</tr>
string str = "select id,branchname from branches";
DataTable dt = new DataTable();
dt = db_function.getDataTable(str);
ddlBranches.DataSource = dt;
ddlBranches.DataTextField = "branchname";
ddlBranches.DataValueField = "id";
ddlBranches.DataBind();
public static DataTable getDataTable(string SQL)
{
if (sqlcon == null || sqlcon.State == ConnectionState.Closed)
{
db_function.opensqlconnection();
}
DataTable dt = new DataTable("ABC");
using (sqlcom = sqlcon.CreateCommand())
{
sqlcom.CommandType = CommandType.Text;
sqlcom.CommandText = SQL;
sqlcom.CommandTimeout = 0;
using (IDataReader reader = sqlcom.ExecuteReader())
{ dt.Load(reader);
if (reader != null) reader.Close();
}
}
return dt;
}
public static DbConnection sqlcon;
public static IDbCommand sqlcom;