I am new to using ASP.NET and i just want to know how to write the SQLDataSource in Code Behind, I hope you can help me. Thanks in advance.
Here is the code :
<asp:SqlDataSource ID="dsRecentCases" runat="server"
ConnectionString="<%$ ConnectionStrings:****ConnectionString %>"
SelectCommand="SELECT TOP 10 C.CASE_KEY, C.DEPARTMENT_CASE_NUMBER,
D.DEPARTMENT_NAME, O.OFFENSE_DESCRIPTION AS CHARGE, LAB_CASE, OFFENSE_DATE
FROM TV_LABCASE C
INNER JOIN TV_DEPTNAME D ON C.DEPARTMENT_CODE = D.DEPARTMENT_CODE
INNER JOIN TV_OFFENSE O ON C.OFFENSE_CODE = O.OFFENSE_CODE
ORDER BY CASE_DATE DESC">
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsDepartment" runat="server"
ConnectionString="<%$ ConnectionStrings:****ConnectionString %>"
SelectCommand="SELECT [DEPARTMENT_CODE], [DEPARTMENT_NAME] FROM [TV_DEPTNAME]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsCharge" runat="server"
ConnectionString="<%$ ConnectionStrings:*****ConnectionString %>"
SelectCommand="SELECT [OFFENSE_CODE], [OFFENSE_DESCRIPTION] FROM [TV_OFFENSE]">
</asp:SqlDataSource>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
string connStr =
ConfigurationManager.ConnectionStrings["****ConnectionString"].ConnectionString;
drpDepartment.DataSource = dsDepartment;
drpDepartment.DataTextField = "DEPARTMENT_NAME";
drpDepartment.DataValueField = "DEPARTMENT_CODE";
drpDepartment.DataBind();
drpCharge.DataSource = dsCharge;
drpCharge.DataTextField = "OFFENSE_DESCRIPTION";
drpCharge.DataValueField = "OFFENSE_CODE";
drpCharge.DataBind();
grdRecentCases.DataSource = dsRecentCases;
grdRecentCases.DataBind();
}