0

Hi I have the following code to insert data into a db

 int svcTypeID = Convert.ToInt32(comboBox1.SelectedValue);

                String svcGuid = label2.Text;

                int heartbeat = Convert.ToInt32(textBox1.Text);

                String hostname = System.Net.Dns.GetHostName();

                Guid gu = new Guid(svcGuid);
                SqlConnection conn = new SqlConnection(@"Data Source=x\x;Initial Catalog=task_system;Integrated Security=True");

                SqlCommand cmd = new SqlCommand("storedProc");
                cmd.Connection = conn;
                cmd.Parameters.AddWithValue("@guid", gu);
                cmd.Parameters.AddWithValue("@id", svcTypeID);
                cmd.Parameters.AddWithValue("@heartbeat", heartbeat);
                cmd.Parameters.AddWithValue("@hostname", "'" + hostname + "'");
                cmd.Parameters.Add(new SqlParameter()
                {
                    ParameterName = "RETURN_VALUE",
                    Direction = ParameterDirection.ReturnValue
                });
                conn.Open();
                cmd.ExecuteNonQuery();

However the data will not insert, the stored procedure that I am using works in SQL when I execute it, so not sure what the issue is. The parts of the stored procedure relevant are:

@guid uniqueidentifier,
 @id int,
 @heartbeat int,
 @hostname nvarchar(MAX)

 IF @@ROWCOUNT < 1
BEGIN
  INSERT INTO [services]
             (guid,
              id,
              started_ts,
              last_contact_ts,
              heartbeat,
              hostname)
  VALUES     (@guid,
              @id,
              GETDATE(),
              GETDATE(),
              @heartbeat,
              @hostname)
END

When I run my code in visual studio it tells me the procedure or function expects parameter @guid which was not supplied, any ideas why it isnt getting the values i'm passing?

2
Contributors
2
Replies
16
Views
2 Years
Discussion Span
Last Post by coder91
1

Hi

You need to specify the type of command you are running. I believe the default is Text which is why you get the exception regarding the missing parameter. Add the following:

cmd.CommandType = CommandType.StoredProcedure;

HTH

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.