-1

in my search function i need to pass two parameters to SP.Here i kept if condition for that.But am not getting required output. here is my code.any one help me

 if (IsValid)
            {
                DataTable dt = new DataTable();

                SqlConnection con = new SqlConnection(myStr);
                SqlCommand cmd = new SqlCommand("spRedemItem", con);
                cmd.CommandType = CommandType.StoredProcedure;
                if(Parameter.Equals(DropDownList2.SelectedValue=="CustomerCode"))
                {
                    cmd.Parameters.AddWithValue("@CustomerCode", txtkey2.Text);

                }
                else
                {
                     cmd.Parameters.AddWithValue("@CustomerName", txtkey2.Text);
                }
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                Session["CustomerName"] = dt;
                con.Open();
                DataSet ds = new DataSet();
                sda.Fill(ds);
                dt = ds.Tables[0];
                Label10.Text = dt.Rows[0]["ItemCode"].ToString();
                Label11.Text = dt.Rows[0]["CustomerName"].ToString();
                Label12.Text = dt.Rows[0]["PointsNeeded"].ToString();
                // Session["CustomerName"] = dt;
                View.DataBind();
                con.Close();
            }
chrish549
  • 91
  • 1
  • 3
  • 11
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jeroen Vannevel Apr 15 '14 at 03:00

1 Answers1

0

If your sproc has two parameters then you need to pass two parameters every time. Generally you would write your SQL code such that you can just pass NULL to any parameters that you want to ignore, e.g. WHERE (@Column1 IS NULL OR Column1 = @Column1). You then use DBNull.Value for the parameter value if you want to ignore that parameter. You can't use AddWithValue though, because a data type can't be inferred.

E.g.

command.CommandText = @"SELECT *
                        FROM MyTable
                        WHERE (@C1 IS NULL OR C1 = @C1)
                        AND (@C2 IS NULL OR C2 = @C2)";
command.Parameters.Add("@C1", SqlDbType.Int).Value = (someValue == "int"
                                                        ? Convert.ToInt32(myTextBox.Text)
                                                        : (object) DBNull.Value);
command.Parameters.Add("@C2", SqlDbType.VarChar, 50).Value = (someValue == "string"
                                                                ? myTextBox.Text
                                                                : (object) DBNull.Value);
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • 1st parameter int,2nd string sir ..my intention is to check in if condition weather the parameter is int or string sir....am not getting it.rest of code in SP similar sir as u suggested like sir – chrish549 Apr 15 '14 at 03:45
  • You don't check whether a single parameter is `int` or `string`. You add both parameters every time, as I said. You just provide NULL for the one you want to ignore. See the code example that I will be adding to my answer. – jmcilhinney Apr 15 '14 at 03:47
  • sir i have a doubt at someValue .at that place what i have to replace i didn't get it sir..will you please tell me.Yesterday i just replaced with Column value.But for all fields am not getting output. – chrish549 Apr 16 '14 at 02:06
  • Try doing some thinking for yourself. The code I provided is an **example**. In my **example** I am testing a condition and either using a value or NULL depending on whether that condition is true or false. That's what you do. It's up to you what condition you test. It's your code. You know what you're trying to achieve so test the condition that achieves it. – jmcilhinney Apr 16 '14 at 02:13