Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

im using the next code:

using (SqlConnection con = new SqlConnection())
  {
      try
      {
          con.ConnectionString = "Data Source=PC;Persist Security Info=True;User ID=sa;Initial Catalog=XXXX; Password=XXXXX";
          con.Open();

          adap = new SqlDataAdapter("select Description, Status, Loc from TableKit where IdStatus = 1 and Idstore = 1", con);

          ds = new System.Data.DataSet();
          adap.Fill(ds, "Idkit_details");
          DataTable dstable = ds.Tables["Idkit_details"];


it Works, but now i need to add to the adap sentence two new parameters as
IdStatus 
and
Idstore 
.

whith different user buttons
IdStatus 
and
Idstore 
gets different states, 1,2,3 but both are a int

how i can change the actually parametres with a variable parameters?

adap = new SqlDataAdapter("select Description, Status, Loc from TableKit where IdStatus = 1 and Idstore = 1", con);


thanks

What I have tried:

adap = new SqlDataAdapter("select Description, Status, Loc from TableKit where IdStatus = 1 and Idstore = 1", con);
Posted

1 solution

try

private void button1_Click(object sender, EventArgs e)
      {
          string statusCommaSeperatedValue = "1,2,3"; // get the value from any control (textbox or listbox or any input)
          string storeId = "1"; // get the value from any input

         DataTable dt =  GetData(statusCommaSeperatedValue,storeId);
      }

      private static DataTable GetData(string statusCommaSeperatedValue, string storeId)
      {
          DataTable dt = new DataTable();

              try
              {
                  SqlConnection con = new SqlConnection();
                  con.ConnectionString = "Data Source=PC;Persist Security Info=True;User ID=sa;Initial Catalog=XXXX; Password=XXXXX";
                  string query = "select Description, Status, Loc from TableKit where IdStatus in ( @status ) and Idstore = @store";
                  SqlCommand cmd = new SqlCommand(query, con);
                  cmd.Parameters.AddWithValue("@status", statusCommaSeperatedValue);
                  cmd.Parameters.AddWithValue("@store", storeId);
                  cmd.CommandType = CommandType.Text;
                  SqlDataAdapter adap = new SqlDataAdapter(cmd);
                  adap.Fill(dt);
              }
              catch (Exception ex)
              {
                  throw;
              }

          return dt;
      }
  }
 
Share this answer
 
Comments
Member 13608602 7-Jan-18 4:47am    
this solution is perfect for my. thank you very much.
Karthik_Mahalingam 7-Jan-18 6:48am    
Welcome
Maciej Los 7-Jan-18 6:37am    
5ed!
Karthik_Mahalingam 7-Jan-18 6:48am    
Thank you maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900