I have a form that a user can fill out which is subsequently written to an sql database upon submit... however, the form is spread across 5 seperate webpages... and I am currently passing the parameters in the url and then using the Request method on each page to read them into strings and send them on as the user clicks through the 5 pages to complete the form...when they get to the 5 th page all the parameters are then submitted to the table.... I'm a novice programmer, and this method I am using seems a bit drawn out... any suggestions or is this the best route?
Also... If one writes data to an SQL database from a c# web form, what code could I use to grab the ID of that row given that was just written given that i have customerID as an identity?
Elias OlneyPosted Mar 22, 2008, 1:20 PM
This is the code I used to solve the problem and get an ID for the item of data I just inserted...
string
query = "insert into Quotation (FirstName) VALUES (@FirstName);" + "Select Scope_Identity()"; string connect = @"Database=printiceland;Trusted_Connection=Yes;"; using (SqlConnection conn = new SqlConnection(connect)){
using (SqlCommand cmd = new SqlCommand(query, conn)){
cmd.Parameters.AddWithValue(
"@FirstName", txtID.Text);conn.Open();
object myID = cmd.ExecuteScalar();Response.Write(myID);
conn.Close();
}
Elias OlneyPosted Mar 21, 2008, 11:03 PM
Thanks... had a look about and msdn have some good advice on what you suggested...
http://msdn2.microsoft.com/en-us/library/59x02y99(vs.71).aspx
Which basically answers my first question, since I can now get a row ID after the first insert ... then just use the ID to perform updates with the input from the form in the other pages.
Many thanks
Ryan AlfordPosted Mar 21, 2008, 9:36 PM
but for your second question, return the "ID" as a return value in a stored procedure(if you are using stored procedures to do the inserts).