Need help with Database insertion

choudhmh

Member
Joined
Jul 18, 2006
Messages
12
Programming Experience
Beginner
Hi there guys,
I'm learning vb.net for the first time - at present i'm playing around with stored procedures. i created this procedure ti insert data into database - but i'm not sure how do i create the back end code and the parameters. I looked of on different tutorial still cannot find the answer. Can some one give me an example of how i could do the insertion through stored procedure:
VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=1]ALTER PROCEDURE [/SIZE][/COLOR][/SIZE][SIZE=1][dbo].[usp_InsertSupplierName][/SIZE]
[SIZE=1]@SupplierName [COLOR=#0000ff]ntext[/COLOR],[/SIZE]
[SIZE=1]@SupplierID [/SIZE][COLOR=#0000ff][SIZE=1]int [/SIZE]
[SIZE=1]AS[/SIZE]
[SIZE=1]SET NOCOUNT ON[/SIZE]
[SIZE=1]INSERT INTO [/SIZE][/COLOR][SIZE=1][dbo].[SupplierName] ([/SIZE]
[SIZE=1][SupplierName][/SIZE]
[SIZE=1]) [COLOR=#0000ff]VALUES [/COLOR]([/SIZE]
[SIZE=1]@SupplierName[/SIZE]
[SIZE=1])[/SIZE]
[COLOR=#0000ff][SIZE=1]SET [/SIZE][/COLOR][SIZE=1]@SupplierID = [COLOR=#0000ff]SCOPE_IDENTITY[/COLOR]()[/SIZE]
Web form:
HTML:
<asp:TextBox ID="SupplierID" runat="server"></asp:TextBox><br />
<asp:textbox ID="SupplierName" runat="server"></asp:textbox>
<asp:Button ID="Submit" Text="Submit" runat="server" />


Thanks
 
Last edited by a moderator:
This creates your connection and opens it...

VB.NET:
Dim sqlINS As SqlCommand
Dim sconn AsNew SqlConnection
sconn = New SqlConnection("Server=FROZEN;Database=master;User ID=sa;Password=today;Trusted_Connection=False")
sconn.Open()

This is how you create a SQL command...

VB.NET:
sqlINS = New SqlCommand("INSERT_CREATIVE_BRIEF2", sconn)
sqlINS.Connection = sconn
sqlINS.CommandType = CommandType.StoredProcedure

"INSERT_CREATIVE_BRIEF2" is the name of the stored procedure

This is how you declare a stored procedure parameter...

VB.NET:
sqlINS.Parameters.Add(New SqlParameter("@ProjectName", SqlDbType.NVarChar, 50))
sqlINS.Parameters("@ProjectName").Value = txtProjectName.Text

Do this for each parameter...

This is how you execute the stored procedure...

VB.NET:
sqlINS.ExecuteNonQuery()
sconn.Close()

I might be missing something. Let me know if this doesnt help out
 
Last edited by a moderator:
Sorry for the late reply was on holiday.
I've tried your solution nearly there, just one issue the SupplierID in the database is auto number but when i execute the script this is what i get:

'usp_InsertSupplierName' expects parameter '@SupplierID', which was not supplied.

Looking back at my stored procedure how can i solve this.

Thanks
 
Back
Top