Monday, March 19, 2012

Calling a Stored Procedure that does an insert and returns one value

Hi !

I have trying to do an insert with a subroutine that calls a stored procedure, but it doesn' t run. The page loads properly but nothing is inserted in the table of the database, no errors appears after submit the form.

<asp:Button id="SubmitButton" OnClick="Send_data" Text="Submit" runat="server"/>

Here is the code:


Sub Send_data(Sender As Object, E As EventArgs)

Dim CmdInsert As New SqlCommand("new_user1", strConnection)
CmdInsert.CommandType = CommandType.StoredProcedure

Dim InsertForm As New SqlDataAdapter()
InsertForm.InsertCommand = CmdInsert

CmdInsert.Parameters.Add(New SqlParameter("@.RETURN_VALUE", SqlDbType.bigint, 8, "User_id"))
CmdInsert.Parameters("@.RETURN_VALUE").Direction = ParameterDirection.ReturnValue

CmdInsert.Parameters.Add(New SqlParameter("@.e_mail", SqlDbType.varchar, 50, "e_mail"))
CmdInsert.Parameters("@.e_mail").Value = mail.Text()

End Sub

What it lacks?cmdInsert.ExecuteNonQuery|||Now when I submit the form the error is:

System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is Closed.

But the connection is available because in my webform appears several 'dropdownlists' with data from the database... And the 'strConnection' is the correct 'SqlConnection' declared before this subroutine like this: Dim strConnection As New SqlConnection(ConfigurationSettings.AppSettings("myConn"))|||If I put this runs:


...
strConnection.open()
cmdInsertar.ExecuteNonQuery
strConnection.close()

End Sub

Is this a good way of doing the insert with the called SP?
Why is not enough with this?:


Dim CmdInsert As New SqlCommand("new_user1", strConnection)

No comments:

Post a Comment