I've searched this site, and other sites to understand and get rid of this exception. Many answers, but I can't find where "I'm wrong". This i the exception:
The parameterized query '(@listenr int,@stregko nvarchar(4000))SELECT * FROM Indkøbsliste' expects the parameter '@stregko', which was not supplied.
And here i my code.
public Indkøbsliste findIndkøbslisteStregkode(string stregko, int listenr)
{
Indkøbsliste inl = new Indkøbsliste();
SqlConnection myCon = DBcon.getInstance().conn();
myCon.Open();
string query = "SELECT * FROM Indkøbsliste WHERE Stregkode = @stregko AND ListeNr = @listenr";
SqlCommand com = new SqlCommand(query, myCon);
com.Parameters.Add("@stregko", System.Data.SqlDbType.VarChar).Value = stregko;
com.Parameters.Add("@listenr", System.Data.SqlDbType.Int).Value = listenr;
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
inl.ListeNr = dr.GetInt32(1);
inl.Stregkode = dr.GetString(2);
inl.Navn = dr.GetString(3);
inl.Antal = dr.GetInt32(4);
inl.Pris = dr.GetDecimal(5);
}
myCon.Close();
return inl;
}
Make use of AddWithValue
method
com.Parameters.AddWithValue("@stregko", stregko);
or
com.Parameters.Add("@stregko", SqlDbType.NVarChar, 50).Value = stregko;
or
SqlParameter parameter = new SqlParameter("@stregko", SqlDbType.NVarChar, 50);
parameter.Value = stregko;
com.Parameters.Add(parameter);
rather than
com.Parameters.Add("@stregko", System.Data.SqlDbType.VarChar).Value = stregko;
The SQL query expects a unicode string for stregko
but you provide a non-unicode string.
Try to change your c# code for assigning the parameter as follows:
com.Parameters.Add("@stregko", System.Data.SqlDbType.NVarChar).Value = stregko;
Note the "N" in NVarChar!
您的SQL参数是nvarchar
尝试在C#代码中使用System.Data.SqlDbType.NVarChar
。
as from your error message, try to change:
com.Parameters.Add("@stregko", System.Data.SqlDbType.VarChar)
with
com.Parameters.Add("@stregko", System.Data.SqlDbType.NVarChar)
您需要指定大小。
com.Parameters.Add("@stregko", System.Data.SqlDbType.NVarChar,50).Value = stregko;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.