GabrielVa
GabrielVa

Reputation: 2388

Running a Stored Procedure in C# Button

Is there a way when clicked on a button in a C# Windows App, that it can fire off a stored procedure to update data? Im also having issues seeing the SP in my data sources, first time this has ever happened to me. Amy ideas on these things? Should I use an SSIS instead, is that even possible?

Thanks

ALTER PROCEDURE dbo.InsertData

 @QuoteNumber as varchar(11), 
 @ItemNumber as varchar(15)
AS
  UPDATE       SF1411
 SET                QuoteNumber = @QuoteNumber, ItemNumber = @ItemNumber, DeleteItem = 'NO'
 WHERE        (QuoteNumber IS NULL)
 RETURN

Upvotes: 0

Views: 2504

Answers (3)

serhat_pehlivanoglu
serhat_pehlivanoglu

Reputation: 982

shin's answer looks ok. although i dont think you have to write "new sql parameter".

it should work like this = cmd.Parameters.Add("@QuoteNumber ", QuoteNumber ));

Upvotes: 1

Jason
Jason

Reputation: 3689

You can use something similar to this in the event handler for the click event of the button, replacing ConnectionString, quote and item with the appropriate values.

using (SqlConnection dataConn = new SqlConnection(ConnectionString))
        {
            dataConn.Open();

            using (SqlCommand dataCommand = dataConn.CreateCommand())
            {
                dataCommand.CommandType = CommandType.StoredProcedure;
                dataCommand.CommandText = "InsertData";

                dataCommand.Parameters.AddWithValue("@QuoteNumber", quote); 
                dataCommand.Parameters.AddWithValue("@ItemNumber", item); 

                dataCommand.ExecuteNonQuery();
            }
        }

Upvotes: 0

shin
shin

Reputation: 666

I think it's something like this...

using (SqlCommand cmd  = new SqlCommand("InsertData", conn))
{
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add(new SqlParameter("@QuoteNumber ", QuoteNumber ));
   cmd.Parameters.Add(new SqlParameter("@ItemNumber ", ItemNumber ));
   cmd.ExecuteNonQuery();
}

Upvotes: 1

Related Questions