简体   繁体   中英

Do I have to create a datasource in aspx? Can I do sqldatasource (sql SPROC), parameters and databind in C#

First, I'll apologize for a newbie question, but I can't seem to find a good example of what I need to do. I have been having trouble with datatypes and my stored procedures when I attempt to create a sqlDataSource in my aspx page and assign session parametersof type int. I wanted to try to do the whole thing in the codebehind so it is easier to debug. It just seems very confusing to me to do part of the work in the aspx, and part in the codebehind. I would be interested in hearing oppinions on this, but I have code here that shows the current state of my attempt. Could you possibly help me get it working?

protected void DoReport()
{
  int ClinicID = Convert.ToInt32(Session["selectedClinic"]);
  String connstr = System.Configuration.ConfigurationManager.ConnectionStrings 
        ["PC3PaymentConnection"].ConnectionString;
       using (SqlConnection conn = new SqlConnection(connstr))
       {
       using ( SqlCommand cmdMeasureHist = new SqlCommand("GetMeasureDetailHistory", conn))
        {
        cmdMeasureHist.CommandType = CommandType.StoredProcedure;
        SqlParameter pclinic = cmdMeasureHist.Parameters.Add("@ClinicID", SqlDbType.Int);
        pclinic.Value = ClinicID;
        SqlParameter pCMSMID = cmdMeasureHist.Parameters.Add("@CMMeasureID", SqlDbType.Int);
        pCMSMID.Value = Convert.ToInt32(ddMeasures.SelectedValue);
        SqlDataSource DsMeasureHist = new SqlDataSource();
        gvHistory.DataSourceID = "DsMeasureHist";
        conn.Open();
        DsMeasureHist.ExecuteNonQuery();
  gvHistory.DataBind();
   }

I just don't understand how to hook the command to the datasource to the gridview. Please help!

You need to use SqlDataAdapter for this, ExecuteNonQuery() is usually for executing insert,update,delete commands. If you want a datasource you'll either use DataReader or DataAdapter. Check the following example:

using ( SqlCommand cmdMeasureHist = new SqlCommand("GetMeasureDetailHistory", conn))
        {
        cmdMeasureHist.CommandType = CommandType.StoredProcedure;
        SqlParameter pclinic = cmdMeasureHist.Parameters.Add("@ClinicID", SqlDbType.Int);
        pclinic.Value = ClinicID;
        SqlParameter pCMSMID = cmdMeasureHist.Parameters.Add("@CMMeasureID", SqlDbType.Int);
        pCMSMID.Value = Convert.ToInt32(ddMeasures.SelectedValue);

SqlDataAdapter da = new SqlDataAdapter(cmdMeasureHist); DataTable dt = new DataTable(); da.Fill(dt); gvHistory.DataSource = dt; gvHistory.DataBind();

Here is an example of how to define everything in the SQLDataSource. You'll probably need to fine tune this a bit more but hopefully it will get you in the right direction.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:PC3PaymentConnection %>" 
        SelectCommand="GetMeasureDetailHistory"
SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter Name="CMMeasureID"  ControlID="ddMeasures" 
PropertyName="SelectedValue" Type="Int32" />            
            <asp:SessionParameter Name="ClinicID" 
SessionField="selectedClinic" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM