SqlConnection connection =newSqlConnection(ConnectionString);
SqlCommand cmd =newSqlCommand(@."INSERT INTO CreditAppFile (GI_RB_Div) VALUES(@.GI_RB_Div) SELECT @.@.idientity as 'NewID'", connection);
cmd.Parameters.Add("@.ID", SqlDbType.BigInt, 8).Value = NewID.Text;
.
.
Response.Redirect(DisplayPage.aspx?ID=@.ID);
i want the ID of the most recently added record to be passed to another page to show all entered data from the prior page... am i close?
A general way is to use a stored procedure to do the INSERT and return the value of the ID via OUTPUT Parameter. Also, please use SCOPE_IDENTITY() instead of @.@.IDENTITY.
From Books On Line:
SCOPE_IDENTITY and @.@.IDENTITY return the last identity values that are generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @.@.IDENTITY is not limited to a specific scope.
|||Check the second part of this article:http://dotnetjunkies.com/WebLog/dinakar/articles/74220.aspx to see some sample code for using stored procedures with OUTPUT parameters.
|||Like Dinakar, my preference is for using stored procedures, but that is not always possible. You can pass two consecutive SQL commands to the command object as you have (almost) done in your example, but they must be separated by a semi-colon. The only parameter you need to add to the command object is the one for @.GI_RB_Div. Then, using Scope_Identity(), your code would look like this:
string connstr = WebConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query = "INSERT INTO CreditAppFile (GI_RB_Div) VALUES(@.GI_RB_Div); Select Scope_Identity();";
SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@.GI_RB_Div", <Value>);
conn.Open();
string NewID = cmd.ExecuteScalar().ToString();
conn.Close();
Server.Transfer("DisplayPage.aspx?ID=" + NewID);
Compiler Error Message:CS1501: No overload for method 'AddWithValue' takes '3' arguments
Source Error:
Line 203: tryLine 204: {Line 205: cmd.Parameters.AddWithValue("@.GI_RB_Div", SqlDbType.VarChar, 50).Value = GI_RB_Div.SelectedValue;Line 206: cmd.Parameters.AddWithValue("@.GI_RB_ExistCust", SqlDbType.VarChar, 50).Value = GI_RB_ExistCust.SelectedValue;Line 207: cmd.Parameters.AddWithValue("@.GI_TB_LegalFirmName", SqlDbType.VarChar, 50).Value = GI_TB_LegalFirmName.Text;
is my error...... any suggesstions?
thanks!!!
|||You are mixing the syntax of Add, and AddWithValue.
AddWithValue takes a parameters name, and a value and it tries to guess what the datatype should be based on the value. Most of the time it works, sometimes it doesn't do exactly what you want/expect, so I recommend using Add instead and specifying the type.
This is how you would do it in VB.NET, you'll need to convert it to C#:
cmd.Parameters.Add("@.GI_RB_Div", SqlDbType.VarChar, 50).Value = GI_RB_Div.SelectedValue
cmd.Parameters.Add("@.GI_RB_ExistCust", SqlDbType.VarChar, 50).Value = GI_RB_ExistCust.SelectedValue
cmd.Parameters.Add("@.GI_TB_LegalFirmName", SqlDbType.VarChar, 50).Value = GI_TB_LegalFirmName.Text
No comments:
Post a Comment