Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
DTFan
Yak Posting Veteran
52 Posts |
Posted - 2006-01-11 : 09:35:20
|
OK. I've searched and thought I was doing it right but, well, obviously I'm not if I'm posting this question Anyway, here is what I am trying to accomplish. I have to do an insert int tblStudent and, after the insert, I have to return to my form (.NET 2005), grab the contents of a listbox, and then run multiple inserts on a second table using the primary key for the Student that was just entered. I am using a stored procedure to do the initial insert and I am trying to return the primary key using Scope_Identity. But I don't know if I'm calling it wrong or if I'm setting it to the variable improperly. Here's the stored procedure that I current have:' stored procedureCREATE PROCEDURE admin_sp_InsertStudent( @in_strStudFName VARCHAR(35), @in_strStudLName VARCHAR(35), @in_strStudEmail VARCHAR(100), @in_intStudPhone BIGINT, @out_intIdentity INTEGER OUTPUT)ASDECLARE @ERR INTSET NOCOUNT ONBEGIN INSERT INTO tblStudent (studFName, studLName, studEmail, studPhone) VALUES (@in_strStudFName, @in_strStudLName, @in_studEmail, @in_intStudPhone) SELECT @out_intIdentity = SCOPE_IDENTITY(), @ERR = @@ERROREND and the .NET code (Visual Basic):' .NET code to call stored procedure and fill intStudID Dim param1 As New SqlClient.SqlParameter sqlCmd = New SqlClient.SqlCommand("admin_sp_InsertStudent", sqlConn) With sqlCmd .CommandType = CommandType.StoredProcedure .Parameters.AddWithValue("@in_strStudFName", Me.txtStudFName.Text.Trim) .Parameters.AddWithValue("@in_strStudLName", Me.txtStudLName.Text.Trim) .Parameters.AddWithValue("@in_strEmail", Me.txtEmail.Text.Trim) .Parameters.AddWithValue("@in_intPhone", Me.txtPhone.Text.Trim) .Parameters.AddWithValue("@out_intIdentity", SqlDbType.Int) .Parameters("@out_intIdentity").Direction = ParameterDirection.Output param1.ParameterName = "@out_intIdentity" Try .Connection.Open() sqlCmd.ExecuteScalar() Catch ex As Exception MessageBox.Show(ex.Message) Finally If .Connection.State = ConnectionState.Open Then .Connection.Close() End If End Try End With MessageBox.Show(param1.ToString)The .ExecuteScalar function in .NET should return 1 and only 1 result so I think that should work for the return value of Scope_Identity(). But When I run this code all I get is a messagebox show @out_intIdentity. Is the problem with the stored procedure, the setting of the output variable, or is it somewhere in my .NET code? Like I said, all I wanna do is insert the record and then return the value for the primary key (and set that to a variable so I can use it later in the code to run the insert for the second table on).Thank you in advance for any and all help that you can give.DTFanEver-hopeful programmer-in-training  |
|
|
jhermiz
3564 Posts |
Posted - 2006-01-11 : 11:05:00
|
Here is an example for you:sproc:CREATE PROC insert_ticket ( @ClientID integer, @FacilityID bigint=NULL, @CategoryID bigint=NULL, @PriorityID varchar(50), @CreatedBy bigint, @TargetDate datetime, @Ticket varchar(5000), @TicketResolution varchar(5000) )ASINSERT INTO Ticket( ClientID, FacilityID, CategoryID, PriorityID, CreatedBy, TargetDate, Ticket, TicketResolution ) SELECT @ClientID, @FacilityID, @CategoryID, @PriorityID, @CreatedBy, @TargetDate, @Ticket, @TicketResolutionRETURN SCOPE_IDENTITY()GO And the .net code:Dim conMyData As SqlConnection Dim cmdInsert As SqlCommand Dim parmReturnValue As SqlParameter Dim intResult As Long Try conMyData = New SqlConnection(ConfigurationSettings.AppSettings("strConn")) cmdInsert = New SqlCommand("insert_ticket", conMyData) With cmdInsert .CommandType = CommandType.StoredProcedure parmReturnValue = .Parameters.Add("RETURN_VALUE", SqlDbType.BigInt) parmReturnValue.Direction = ParameterDirection.ReturnValue 'add the parameters .Parameters.Add("@ClientID", SqlDbType.Int).Value = Me.ddlClient.SelectedItem.Value If (Me.ddlFacility.SelectedIndex = -1) Then .Parameters.Add("@FacilityID", SqlDbType.BigInt).Value = Nothing Else .Parameters.Add("@FacilityID", SqlDbType.BigInt).Value = Me.ddlFacility.SelectedItem.Value End If If (Me.ddlCategory.SelectedIndex = -1) Then .Parameters.Add("@CategoryID", SqlDbType.BigInt).Value = Nothing Else .Parameters.Add("@CategoryID", SqlDbType.BigInt).Value = Me.ddlCategory.SelectedItem.Value End If If (Me.ddlPriority.SelectedIndex = -1) Then .Parameters.Add("@PriorityID", SqlDbType.VarChar).Value = Nothing Else .Parameters.Add("@PriorityID", SqlDbType.VarChar).Value = Me.ddlPriority.SelectedItem.Value End If 'now we check if an admin is assigning 'the call for a user... 'this may happen if the user calls the admin department 'rather than using the tool If Me.tblAdmin.Visible = True Then 'its an admin 'now check if its for a user If Me.rbUser.Checked = True Then 'admin assigning to a user .Parameters.Add("@CreatedBy", SqlDbType.BigInt).Value = CType(Me.ddlUsers.SelectedValue, Long) Else 'self assign .Parameters.Add("@CreatedBy", SqlDbType.BigInt).Value = lngLoginID End If Else 'its only a user so dont even bother... .Parameters.Add("@CreatedBy", SqlDbType.BigInt).Value = lngLoginID End If .Parameters.Add("@TargetDate", SqlDbType.DateTime).Value = Me.lblTargetDate.Text .Parameters.Add("@Ticket", SqlDbType.VarChar).Value = Me.txtTicketDescription.Text .Parameters.Add("@TicketResolution", SqlDbType.VarChar).Value = Me.txtTicketResolution.Text conMyData.Open() .ExecuteNonQuery() intResult = CType(.Parameters("RETURN_VALUE").Value, Long) End With InsertTicket = intResult Catch e As Exception Response.Write("An Error Occurred: " & e.ToString()) InsertTicket = 0 'clean up and close resources Finally cmdInsert = Nothing conMyData.Close() conMyData = Nothing End Try Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url] |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-01-11 : 11:20:05
|
| You should always test a stored procedure in Query analyzer first to verify that it is working the way you expect.If it is, then the problem is in the database call or elsewhere in your .NET code.CODO ERGO SUM |
 |
|
|
DTFan
Yak Posting Veteran
52 Posts |
Posted - 2006-01-11 : 12:52:45
|
jhermiz - Thank you for that. That is a perfect example of what I was trying to do (with some additional stuff thrown in). That'll give me something to look at and try to learn from :)MVJ - You are correct and I did do that. But I wasn't sure if there was some kind of special command that you had to run in order for the Output Parameter to be returned (like in VB, there is console.write (or something like that)).Thank you both for the help and assistance. I'll be implementing that stuff right now DTFanEver-hopeful programmer-in-training |
 |
|
|
|
|
|
|
|