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 |
jhermiz
3564 Posts |
Posted - 2004-09-23 : 13:15:40
|
All Im trying to do is a simple insert of a user name and a client ID into a table when a user logs into my system. This has a datetime stamp field to tell me when the user has logged in.If I execute the sproc in QA it works fine...But in asp.net I have: Function SetSessionData(ByVal lngID As Long) Dim conMyData As SqlConnection Dim cmdSelect As SqlCommand Dim cmdInsert As SqlCommand Dim dtrLoginData As SqlDataReader 'try and make a connection Try conMyData = New SqlConnection(ConfigurationSettings.AppSettings("strConn")) cmdSelect = New SqlCommand("select_user_session_data", conMyData) With cmdSelect .CommandType = CommandType.StoredProcedure .Parameters.Add("@LoginID", SqlDbType.BigInt).Value = lngID conMyData.Open() dtrLoginData = .ExecuteReader() End With 'you MUST call Read initially once before using any of the data If dtrLoginData.Read() Then 'assign session data 'do an insert into the userlogs table cmdInsert = New SqlCommand("insert_user_login", conMyData) With cmdInsert .CommandType = CommandType.StoredProcedure .Parameters.Add("@Login", SqlDbType.VarChar).Value = dtrLoginData("Login") .Parameters.Add("@ClientID", SqlDbType.Int).Value = dtrLoginData("ClientID") .ExecuteNonQuery() End With Session("LoginID") = dtrLoginData("LoginID") Session("Login") = dtrLoginData("Login") Session("ClientID") = dtrLoginData("ClientID") Session("Client") = dtrLoginData("Client") Else 'do nothing End If 'catch any exceptions that might be thrown Catch e As Exception Response.Write("An Error Occurred: " & e.ToString()) 'clean up and close resources Finally cmdSelect = Nothing cmdInsert = Nothing conMyData.Close() conMyData = Nothing End Try End Function And the insert stored procedure doesnt seem to be inserting anything??Anyone have any idea...I know it sets the session data because I use it on the following page.Thanks,JonJonwww.web-impulse.comCan you dig it: http://www.thecenturoncompany.com/jhermiz/blog/ |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-09-23 : 13:36:03
|
If no one can help you on the asp.net part, run SQL Profiler to determine if you are getting an exception.Tara |
 |
|
jhermiz
3564 Posts |
Posted - 2004-09-23 : 14:03:01
|
quote: Originally posted by tduggan If no one can help you on the asp.net part, run SQL Profiler to determine if you are getting an exception.Tara
Hi Tara,Thank You, I decided to seperate the insert into another function and that seemed to work. ASP.net is kinda cool, learning a lot here...there's just a lot of stuff!Thanks though,JonJonwww.web-impulse.comCan you dig it: http://www.thecenturoncompany.com/jhermiz/blog/ |
 |
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2004-09-24 : 11:14:47
|
Jon, the reason that doesn't work is this.The Datareader keeps that connection open and locked until you close the connection and the reader. That's one of the reasons the reader is fast. Create a new connection object and pass that into your Insert Command object, and it will work.Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
jhermiz
3564 Posts |
Posted - 2004-09-24 : 11:19:12
|
HI Mike,BUt that seems redundant to create two seperate connection objects?I mean I have it working but would like to learn why it didnt last time.I ended up creating a new function and doing it in there. |
 |
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2004-09-24 : 11:45:49
|
If you did your Read(), assign the values, close connection(), reopen connection, run insert, then it would have worked.You can't reuse a connection that was used with a datareader.Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
jhermiz
3564 Posts |
Posted - 2004-09-24 : 11:46:27
|
Aha makes sense.Thanks. |
 |
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-09-25 : 10:03:51
|
quote: But that seems redundant to create two seperate connection objects?
If I am understaning your logic why not just execute a scalar or nonquery here and make your sp return output parameters? This is a very efficient way of performing this type of functionality which requires one connection and no datareader..Here is an example: Protected Sub SetSessionData(ByVal lngID As Long) ' Create Instance of Connection and Command Object Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("strConn")) Dim myCommand As SqlCommand = New SqlCommand("SPWithOutputParameters", myConnection) ' Mark the Command as a SPROC myCommand.CommandType = CommandType.StoredProcedure ' Add Input Parameter to SPROC Dim parameterLoginID As SqlParameter = New SqlParameter("@LoginID", SqlDbType.BigInt, 0) parameterLoginID.Value = lngID myCommand.Parameters.Add(parameterLoginID) ' Define Output Parameters Dim parameterLogin As SqlParameter = New SqlParameter("@Login", SqlDbType.VarChar, 50) parameterLogin .Direction = ParameterDirection.Output myCommand.Parameters.Add(parameterLogin) Dim parameterClientID As SqlParameter = New SqlParameter("@ClientID", SqlDbType.Int, 8) parameterClientID .Direction = ParameterDirection.Output myCommand.Parameters.Add(parameterClientID) Dim parameterClient As SqlParameter = New SqlParameter("@Client", SqlDbType.VarChar, 50) parameterClient .Direction = ParameterDirection.Output myCommand.Parameters.Add(parameterClient) Try myConnection.Open() myCommand.ExecuteNonQuery() myConnection.Close() ' Populate session using Output Params from sproc Session("LoginID") = lngID Session("Login") = CStr(parameterLogin).Value Session("ClientID") = CStr(parameterClientID).Value Session("Client") = CStr(parameterClient).Value Catch ex As Exception Dim TheError TheError= Err.Description() Response.Write("An Error Occurred: " & TheError ) End TryEnd Sub |
 |
|
|
|
|
|
|