|
-
Oct 5th, 2015, 08:38 AM
#1
Thread Starter
Junior Member
How to Get the New ID number to pass using Stored Procedure and SCOPE_IDENTITY
I need help! I am able to get this code to work, but it passes the previous Meeting ID, not the newly added Meeting ID. I am still somewhat new to VB and Asp.net so I cannot figure out where the problem is. Maybe the ordering of my code is keeping the newly ID from being passed.
Here is my vb code snippet:
Code:
Partial Class _NewMeetings
Inherits System.Web.UI.Page
Dim WithEvents ftrInsert As New Button
Protected Sub GridView2_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If e.CommandName = "ftrInsert" Then
'call the insert sub (sender is the gridview that called the rowCommand,
gvQuickInsert(sender, SqlDataSource2)
End If
End Sub
Protected Sub gvQuickInsert(ByRef gv As GridView, ByRef ds As SqlDataSource)
Dim paramsArr As ArrayList = New ArrayList
Dim controlsArr As ArrayList = New ArrayList
Dim col As Integer = 0
Dim footrow As GridViewRow = gv.FooterRow
Try
Dim ix As Integer = 0
For ix = 0 To gv.Columns.Count
Dim d As DataControlField = gv.Columns(ix)
If d.InsertVisible = True Then
If footrow.Cells(ix).Controls(1) IsNot Nothing Then
paramsArr.Add(d.SortExpression.ToString)
687: controlsArr.Add(footrow.Cells(ix).Controls(1))
End If
End If
Next
Catch ex As Exception
End Try
ds.InsertParameters.Clear()
Dim ii As Integer = 0
For Each contr As Object In controlsArr
If contr.GetType.Name.ToLower.Contains("textbox") Then
ds.InsertParameters.Add(paramsArr(ii), TryCast(contr, TextBox).Text)
ElseIf contr.GetType.Name.ToLower.Contains("dropdownlist") Then
ds.InsertParameters.Add(paramsArr(ii), _
TryCast(contr, DropDownList).SelectedValue)
ElseIf contr.GetType.Name.ToLower.Contains("checkbox") Then
If TryCast(contr, CheckBox).Checked Then
ds.InsertParameters.Add(paramsArr(ii), 1)
Else
ds.InsertParameters.Add(paramsArr(ii), 0)
End If
End If
ii = ii + 1
Next
ds.Insert()
gv.DataBind()
End Sub
Protected Sub ftrInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ftrInsert.Click
Using conn As New SqlConnection("Data Source=c3digpssql2;Initial Catalog=ServiceLine_Committee;Integrated Security=True;")
Using cmd As New SqlCommand("InsertVtg", conn)
cmd.CommandType = CommandType.StoredProcedure
'cmd.Parameters.AddWithValue("@CID", CID.Text)
'cmd.Parameters.AddWithValue("@Member", Member.Text)
cmd.Parameters.Add("@MID", SqlDbType.Int, 0, "ID")
cmd.Parameters("@MID").Direction = ParameterDirection.Output
conn.Open()
cmd.ExecuteNonQuery()
ID = cmd.Parameters("@MID").Value
End Using
End Using
End Sub
Here is my stored procedure (InsertVtg):
Code:
ALTER PROCEDURE [dbo].[InsertVtg]
(
@MID INT OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO ServiceLine_Committee.dbo.tblVotings (Meeting_ID, Committee_ID, Member_Name)
SELECT M.ID, C.ID, CM.FULL_NAME
FROM ServiceLine_Committee.DBO.tblCommitteeName C
inner join ServiceLine_Committee.DBO.tblMeetings M on M.ContractCategory = C.Contract_Category AND M.Committee = C.Committee_Name
inner join ServiceLine_Committee.DBO.tblCommitteeMembers CM on CM.Committee = M.Committee
SET @MID = SCOPE_IDENTITY()
RETURN @MID
END
My SQL DataSource Code for inserting to tblMeetings table:
Code:
INSERT INTO [tblMeetings] ([Meeting_Date], [Committee], [Meeting_Type], [Voting_Item], [Motioned_By], [Seconded_By], [Comments], [ContractCategory], [ContractVendor], [Submitted_By], [Part_Vendor], [Recomm_Vendor]) VALUES (@Meeting_Date, @Committee, @Meeting_Type, @Voting_Item, @Motioned_By, @Seconded_By, @Comments, @ContractCategory, @ContractVendor, @Submitted_By, @Part_Vendor, @Recomm_Vendor)
Last edited by deedv; Oct 5th, 2015 at 04:04 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|