Reputation: 191
I'm trying to insert the data based on the id and I have added the id in parameter but the error still show that id was not supplied.
public static int ExecuteNonQuery(string sql, CommandType type, params SqlParameter[] param)
{
using (SqlConnection conn = new SqlConnection(GetConnectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.StoredProcedure;
PrepareCommand(sql, conn, cmd, type, param);
return cmd.ExecuteNonQuery(); // Error here (unhandled exception)
}
}
Insert data function
public int AddUsers(Users user)
{
int rel = 0;
string sql = "INSERT INTO Student
VALUES (@id, @groupId, @userInfo, @imagePath)";
SqlParameter[] param = {
new SqlParameter("@id", user.uid),
new SqlParameter("@groupId", user.groupId),
new SqlParameter("@userInfo",user.userInfo),
new SqlParameter("@imagePath",user.imagePath),
};
rel = SqlHelper.ExecuteNonQuery(sql, CommandType.Text, param);
return rel;
}
I will be calling the method if id is not null like example.
if(id != 0)
{
int rel = AddUsers(user);
}
Upvotes: 2
Views: 107
Reputation: 17400
You are checking for id != 0
but use user.uid
as parameter for @id
. What type is user.uid
? And be sure it's != null
because if you add null
as parameter-value, it's treated as not provided. If you need to insert NULL into the database use DbNull.Value
Upvotes: 2