0

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();
    }
3
  • What is not working? Commented Oct 8, 2019 at 16:39
  • learn.microsoft.com/en-us/dotnet/framework/data/adonet/… Commented Oct 8, 2019 at 16:49
  • it is working but i just want to know how can this be written in code behind. can you please help me? Commented Oct 9, 2019 at 2:49

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. It was a bit hard to understand what you were looking for. If you had asked about creating a SqlDataSource control it would have been easy. It's obvious now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.