We have an aspx page which is a has a number of text boxes. We want to take the data entered into these textboxes and update our SQL Server database with them. However if any of the textboxes are left blank then we would like the data to be left as it is.
We have written the following stored procedure to carry out the update:
ALTER PROCEDURE pr_updateBooking
(
@BookingRef INT,
@BoatID INT,
@LeadPassenger INT,
@StartDate Date,
@Duration INT,
@Pets INT,
@Children INT,
@Passengers INT,
@SpecialRequests VARCHAR(255),
@BalanceOutstanding NUMERIC(12, 2),
@Comments VARCHAR(50)
)
AS
DECLARE @error INT
UPDATE BookingView
SET Balance_Outstanding = @BalanceOutstanding,
Comments = @Comments
WHERE Booking_Ref = @BookingRef
UPDATE vBoat_Booking
SET BoatID = @BoatID, Lead_PassengerID = @LeadPassenger,
Start_Date = @StartDate, Duration_In_hours = @Duration,
Number_of_pets = @Pets, Number_of_children = @Children,
Number_of_passengers = @Passengers
WHERE Booking_Ref = @BookingRef
SET @error = @@error
IF @error <> 0
RETURN 99
ELSE
RETURN 0
Here is the C# code which will be run when the submit button is clicked on our aspx page
protected void buttonClicked(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["G4StowawaysConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(CS);
conn.Open();
SqlCommand cmd2 = new SqlCommand("pr_updateBooking", conn);
cmd2.CommandType = CommandType.StoredProcedure;
// add our parameters to our command object
cmd2.Parameters.Add("@BookingRef", SqlDbType.Int).Value = BookingRef.Text;
cmd2.Parameters.Add("@BoatID", SqlDbType.Int).Value = BoatID.Text;
cmd2.Parameters.Add("@LeadPassenger", SqlDbType.Int).Value = LeadPassenger.Text;
cmd2.Parameters.Add("@StartDate", SqlDbType.Date).Value = StartDate.Text;
cmd2.Parameters.Add("@Duration", SqlDbType.Money).Value = Duration.Text;
cmd2.Parameters.Add("@Pets", SqlDbType.Int).Value = Pets.Text;
cmd2.Parameters.Add("@Children", SqlDbType.Int).Value = Children.Text;
cmd2.Parameters.Add("@Passengers", SqlDbType.Int).Value = Passengers.Text;
cmd2.Parameters.Add("@SpecialRequests", SqlDbType.VarChar, 255).Value = SpecialRequests.Text;
cmd2.Parameters.Add("@BalanceOutstanding", SqlDbType.Int).Value = BalanceOutstanding.Text;
cmd2.Parameters.Add("@Comments", SqlDbType.VarChar, 50).Value = Comments.Text;
try
{
if (cmd2.Connection.State == ConnectionState.Closed)
{
cmd2.Connection.Open();
}
cmd2.ExecuteNonQuery();
}
catch (Exception)
{
}
finally
{
cmd2.Connection.Close();
}
}
When we run the page there is no error message however the data is not appearing in the database!?
cmd2.ExecuteNonQuery();? Also you are returning 0 or 99, how are you reading that in your C# code? Going forward please format your content/code properly so that your post is taken more seriously.