jorame
jorame

Reputation: 2207

Error on Insert SqlCommand in ASP.NET

I'm going crazy here, can someone please tell me what's wrong with this code

con.Open();
cmd = new SqlCommand("INSERT INTO COUNTERS(COUNTER_START, COUNTER_CURRENT, COUNTER_NEXT, COUNTER_TYPE, COUNTER_DATE_CREATED, COUNTER_TIME_CREATED, COUNTER_CREATED_BY) " +
                    "VALUES(@counter_start, @counter_current, @counter_next, @counter_type, @date, @time, @user)", con);

cmd.Parameters.Add("@counter_start", SqlDbType.Int).Value = counter_start.Text;
cmd.Parameters.Add("@counter_current", SqlDbType.Int).Value = counter_current.Text;
cmd.Parameters.Add("@counter_next", SqlDbType.Int).Value = counter_next.Text;
cmd.Parameters.Add("@counter_type", SqlDbType.VarChar, 10).Value = counter_type.Text;
cmd.Parameters.Add("@date", SqlDbType.VarChar, 20).Value = date;
cmd.Parameters.Add("@time", SqlDbType.VarChar, 20).Value = time;
cmd.Parameters.Add("@user", SqlDbType.VarChar, 50).Value = user;

cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();

I'm getting this error

The parameterized query '(@counter_start int,@counter_current int,@counter_next int,@coun' expects the parameter '@user', which was not supplied

Any input will be really appreciate it.

Upvotes: 2

Views: 801

Answers (1)

Habib
Habib

Reputation: 223207

You may try debugging the application and see what value is being held by variable user. I think the user you are supplying is null. If you are using C# 4.0 then try

 cmd.Parameters.Add("@user", SqlDbType.VarChar, 50).Value = user ?? DBNull.Value;

or for earlier versions of C#

 cmd.Parameters.Add("@user", SqlDbType.VarChar, 50).Value = user != null ? user : DBNull.Value;

You may also use string.Empty in place of DBNull.Value, if you want an empty string instead of DBNull

Upvotes: 2

Related Questions