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();
}
Hope it may help other people:)
I already formulated the code for C# server-side:
SqlDataSource dsDepartment = new SqlDataSource();
dsDepartment.ID = "dsDepartment";
this.Page.Controls.Add(dsDepartment);
dsDepartment.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dsDepartment.SelectCommand = "SELECT [DEPARTMENT_CODE], [DEPARTMENT_NAME] FROM [TV_DEPTNAME]";
drpDepartment.DataSource = dsDepartment;
drpDepartment.DataTextField = "DEPARTMENT_NAME";
drpDepartment.DataValueField = "DEPARTMENT_CODE";
drpDepartment.DataBind();
SqlDataSource dsCharge = new SqlDataSource();
dsCharge.ID = "dsCharge";
this.Page.Controls.Add(dsCharge);
dsCharge.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dsCharge.SelectCommand = "SELECT [OFFENSE_CODE], [OFFENSE_DESCRIPTION] FROM [TV_OFFENSE]";
drpCharge.DataSource = dsCharge;
drpCharge.DataTextField = "OFFENSE_DESCRIPTION";
drpCharge.DataValueField = "OFFENSE_CODE";
drpCharge.DataBind();
SqlDataSource dsRecentCases = new SqlDataSource();
dsRecentCases.ID = "dsRecentCases";
this.Page.Controls.Add(dsRecentCases);
dsRecentCases.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dsRecentCases.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";
grdRecentCases.DataSource = dsRecentCases;
grdRecentCases.DataBind();
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.