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.'
Ali
  • 31
  • 1
  • 8
  • 2
    Add also the setting for the SqlDbType in the constructor of the parameter with value equal to SqlDbType.NVarChar – Steve Jan 13 '18 at 20:02
  • I did same thing but still end with same error com.Parameters.Add("@idP", SqlDbType.NVarChar,8000).Value=proNames[i]; – Ali Jan 13 '18 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 – Steve Jan 13 '18 at 20:12
  • what is the value of proNames[i]? – Dan Bracuk Jan 13 '18 at 20:15
  • 1
    Please read [MCVE] 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) – Alexei Levenkov Jan 13 '18 at 20:21
  • what is `i`? Is that code running in a loop? – Crowcoder Jan 13 '18 at 21:48
  • yes it is loop. – Ali Jan 14 '18 at 14:25

2 Answers2

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]);
Ehsan Ullah Nazir
  • 1,827
  • 1
  • 14
  • 20
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.

Justinas Marozas
  • 2,482
  • 1
  • 17
  • 37