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?

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

Thank you, this worked :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.