0
com = new SqlCommand("SELECT pro_name from products where pro_name= @idP", con);
                SqlParameter param = new SqlParameter
                {
                    ParameterName = "@idP",
                    Value = proNames[i]
                };
                com.Parameters.Add(param);

it works fine with integer type but for string its not working it give error

System.Data.SqlClient.SqlException: 'The parameterized query '(@idP nvarchar(4000))SELECT pro_name from products where pro_nam' expects the parameter '@idP', which was not supplied.'
7
  • 2
    Add also the setting for the SqlDbType in the constructor of the parameter with value equal to SqlDbType.NVarChar Commented Jan 13, 2018 at 20:02
  • I did same thing but still end with same error com.Parameters.Add("@idP", SqlDbType.NVarChar,8000).Value=proNames[i]; Commented Jan 13, 2018 at 20:09
  • 1
    Well, you are definitively adding that parameter, but if you get that error message then we should look at the remainder of your code when you execute that command to be certain that you haven't skipped something Commented Jan 13, 2018 at 20:12
  • what is the value of proNames[i]? Commented Jan 13, 2018 at 20:15
  • 1
    Please read minimal reproducible example guidance and make sure all necessary code and data present in the sample. I.e. in this case proNames[i] must be replaced with actual value (possibly simplified/sanitized) and actual call to DB shown (as frequently such problems come from constructing one query but using some other one to make actual call) Commented Jan 13, 2018 at 20:21

2 Answers 2

2

Try with passing proper DBType. In your case , this will be NVarChar

Make sure proNames[i] has value , Not Null or EmptyString

SqlParameter param = new SqlParameter()
{
    ParameterName = "@idP",
    SqlDbType = SqlDbType.NVarChar, // your DBType
    Value = proNames[i]
};

You can also try adding parameter as below:

command.Parameters.AddWithValue("@idP", proNames[i]);
Sign up to request clarification or add additional context in comments.

7 Comments

` proNames[i]` has value ?
worked with command.Parameters.AddWithValue("@idP", "proNames[i]"); ?
it gives empty result
Not sure what are you missing now , please look at excepted answer at stackoverflow.com/questions/18316767/…
that accepted answer is worked with integer i want to do with string ......
|
1

You should set SqlDbType and Size if it's VAR* type.

 SqlParameter param = new SqlParameter()
 {
     ParameterName = "@idP",
     SqlDbType = SqlDbType.VarChar,
     Size = 4000,
     Value = proNames[i]
 };

By the way, if you'll want to pass NULL - you'll have to use DbNull.Value instead of just normal C# NULL.

2 Comments

What about SqlDbType.NVarChar?
Is it still saying parameter not supplied?

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.