-1

Procedure or function 'insert_territory' expects parameter '@name', which was not supplied.

public void add_record_territory(territory t)
{
  SqlCommand cmd = new SqlCommand("insert_territory",con);
  cmd.CommandType = CommandType.StoredProcedure;
  cmd.Parameters.AddWithValue("@name",t.Name);
  cmd.Parameters.AddWithValue("@Regdate", DateTime.Now);
  cmd.Parameters.AddWithValue("@Regtime", DateTime.Now);
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
}
Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108
  • 1
    value in the name parameter might be null or empty string. – Muhammad Nasir Jun 10 '19 at 10:37
  • 3
    You're using `SqlCommand` and `SqlConnection` with MySQL? – David Jun 10 '19 at 10:38
  • 1
    As per the comment that @David made, I would switch to using `MySqlConnection` and `MySqlCommand` at the very least. You should always try to use the proper types for things like this. Internally they operate slightly differently depending on what the underlying connection goes to. – gmiley Jun 10 '19 at 10:43
  • I am using sqlmanagemnt studio 2012 – Naflan Mohdeen Jun 10 '19 at 10:45
  • 1
    @NaflanMohdeen: *"I am using sqlmanagemnt studio 2012"* - You're using tools for connecting to and interacting with SQL Server, but the tags on your question indicate that you're connecting to MySQL. If misleading information is given in your question that only makes it more difficult for the community to help. – David Jun 10 '19 at 10:47
  • when i was load the data from database to grid it's work but insert the data i was get error – Naflan Mohdeen Jun 10 '19 at 10:52

2 Answers2

2

You should check if name null then use

DBNull.Value

for "@name" value

command.Parameters.AddWithValue("@application_ex_id",
       ((object)logSearch.LogID) ?? DBNull.Value);
  • Its Work null value was save. i think "" text box not pass the values variable call " public string Name { get; set; } (terriyoru class)" – Naflan Mohdeen Jun 10 '19 at 11:09
0

First make sure "t.Name" is not null

using (SqlConnection con = new SqlConnection(dc.Con))
{
    using (SqlCommand cmd = new SqlCommand("insert_territory", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;        
        cmd.Parameters.AddWithValue("@name",t.Name);
        cmd.Parameters.AddWithValue("@Regdate", DateTime.Now);
        cmd.Parameters.AddWithValue("@Regtime", DateTime.Now);
        con.Open();
        cmd.ExecuteNonQuery();
    }            
}
Black
  • 140
  • 2
  • 10