none
Run SQL Server Parameter Query From C# RRS feed

  • Question

  • What is proper way to run a sql server parameter query from C# and return a dataset?  I have tried the below syntax - but does not appear to be a complete dataset that is returned.

    private void CboBoxChangeEvent(object sender, EventArgs e)
    
            {
    
                DataSet ds;
    
                DataSet ds1 = new DataSet();
    
                txtcn.Visible = true;
    
                lblcn.Visible = true;
    
    
    
                if (cboNewOrOld.Text == "New")
    
                {                
    
                    ds = dal.RunSQLServerStoredProcedure("_GETInfo1");
    
                    AIN = ds.Tables[0];
    
                }
    
     
    
    
    
                if (cboNewOrOld.Text == "OLD")
    
                {
    
                    SqlConnection sqlconn = new SqlConnection(StartupForm.ServerAddressToUse);
    
                    SqlDataAdapter sda = new SqlDataAdapter();
    
                    SqlCommand sqlcmd = new SqlCommand("_GETnfocn", sqlconn);
    
                    sqlcmd.CommandType = CommandType.StoredProcedure;
    
                    sqlcmd.Parameters.AddWithValue("@cn", txtcn.Text);
    
                    sda.SelectCommand = sqlcmd;
    
                    sda.Fill(ds1, "cam");
    
                    dtArtInfoCampaign = ds1.Tables[0];
    
                }
    
            }
    
    
    
            private void cnInput(object sender, EventArgs e)
    
            {
    
                if (txtcn.Text != "")
    
                {
    
                    dgvai.Visible = true;
    
                    btnExportToExcel.Visible = true;
    
                    btnSaveEdits.Visible = true;
    
                    if (cboNewOrOld.Text == "New")
    
                    {
    
    
                       System.Data.DataColumn newColumn = new System.Data.DataColumn("cn", typeof(System.String));
    
                       newColumn.DefaultValue = txtcn.Text;
    
                       AIN.Columns.Add(newColumn);
    
    
                       dgvai.DataSource = AIN;
    
                       dgvai.AutoResizeColumns();
    
                    }
    
                    if (cboNewOrOld.Text == "OLD")
    
                    {
    
                        dgvai.DataSource = AIN;
    
                        dgvai.AutoResizeColumns();
    
                    }
    
                }
    
            }

    Friday, June 15, 2018 8:01 PM

All replies

  • Well it would be good to see the SP definition and the code for dal.RunSQLServerStoredProcedure. It's really simple. See the following demo don't concern yourself that it's web based as the code logic is the same in desktop apps.

    https://www.aspsnippets.com/Articles/Load-Fill-Populate-DataTable-from-Stored-Procedure-in-ASPNet-using-C-and-VBNet.aspx

    or this which is similar code path but does a DataTable (if you are not interested in more than one table)

    DataTable table = new DataTable();
    using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString))
       using(var cmd = new SqlCommand("usp_GetABCD", con))
          using(var da = new SqlDataAdapter(cmd))
          {
             cmd.CommandType = CommandType.StoredProcedure;
             da.Fill(table);
          }
       }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, June 15, 2018 10:29 PM
    Moderator