I need to convert this routine to a storedprocedure that inserts this data and based on the generated autoincrement value I have to make another insert into another table.
I think I need to use a stored proc with the SCOPE_IDENTITY method
When I press the submitbutton the database needs to be updated with data for both tables from input textboxes
This did not work
the sqlcommand asks for the ID parameter, but I thought this was handeled internally?
The same thing could be set about the GetDate() method, it needs to set it automaticly.
What is wrong with the code?
Also
Does a stored proc bypass the tableadapter, and if so, does the disconnected dataset needs to be refreshed again somehow?
Thanks, Richard
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2]dr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DDataSet.Table1Row =[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DDataSet.Table1.NewTable1Row[/SIZE]
[SIZE=2]dr.Name = [/SIZE][SIZE=2][COLOR=#a31515]"asperigo"[/COLOR][/SIZE]
[SIZE=2]dr.Adress = route 66[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DDataSet.Table1.AddTable1Row(dr)[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Table1TableAdapter.Update([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DDataSet)[/SIZE]
When I press the submitbutton the database needs to be updated with data for both tables from input textboxes
This did not work
the sqlcommand asks for the ID parameter, but I thought this was handeled internally?
The same thing could be set about the GetDate() method, it needs to set it automaticly.
What is wrong with the code?
VB.NET:
Create Procedure InsertTwoTables
@Name As varchar(20),
@Adress As varchar(20),
@IDTable2 as int
@Field2Table2 As datetime= getDate()
@Field3Table2 As varchar(20),
@NewId As int Output
As
Insert Into Table1 (Name, Adress)
Values (@Name, @Adress)
Set @NewId = Scope_Identity
Insert Into Table2 (ID, Field2Table2, Field3Table2)
Values (@NewId , @Field2Table2 , @Field3Table2)
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fn[/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TBox1.Text[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ln [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2]= [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TBox2.Text[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] str [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2]= [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TBox3.Text[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] conn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlConnection = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlConnection(sconn)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cmd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlCommand = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlCommand([/SIZE][SIZE=2][COLOR=#a31515]"InsertTwoTables"[/COLOR][/SIZE][SIZE=2], conn)[/SIZE]
[SIZE=2]cmd.CommandType = CommandType.StoredProcedure[/SIZE]
[SIZE=2]conn.Open()[/SIZE]
[SIZE=2]cmd.Parameters.AddWithValue([/SIZE][SIZE=2][COLOR=#a31515]"@FirstName"[/COLOR][/SIZE][SIZE=2], fn)[/SIZE]
[SIZE=2]cmd.Parameters.AddWithValue([/SIZE][SIZE=2][COLOR=#a31515]"@LastName"[/COLOR][/SIZE][SIZE=2], ln)[/SIZE]
[SIZE=2]cmd.Parameters.AddWithValue([/SIZE][SIZE=2][COLOR=#a31515]"@Field3Table2"[/COLOR][/SIZE][SIZE=2], "something")[/SIZE]
[SIZE=2]cmd.ExecuteNonQuery()[/SIZE]
[SIZE=2]conn.Close()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
Does a stored proc bypass the tableadapter, and if so, does the disconnected dataset needs to be refreshed again somehow?
Thanks, Richard
Last edited: