Dim strSQL As String = ""
strSQL = "" & _
"UPDATE news_users " & _
"SET newsuser_status = @newsuser_status " & _
"WHERE newsuser_mail = @newsuser_mail " & _
"AND newsuser_pass = @newsuser_pass"
Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MasterConnStr").ConnectionString)
Using updatecmd As OleDbCommand = New OleDbCommand(strSQL, connection)
updatecmd.CommandType = CommandType.Text
updatecmd.Parameters.AddWithValue("@newsuser_status", 1)
updatecmd.Parameters.AddWithValue("@newsuser_mail", TextMail)
updatecmd.Parameters.AddWithValue("@newsuser_pass", TextPass)
updatecmd.Connection.Open()
updatecmd.ExecuteNonQuery()
End Using
End Using
Now i will add an error code handler to it, so i can response.write an error if i get an error I have looked at this code, but i dont know if thats the best to use and then if so, where du i place it !?
Dim i As Integer = CInt(updatecmd.ExecuteNonQuery())
If i = 0 Then
' here you can give the user mesage that wrong ......
End If
Your error handling should go around the Connection.Open call:
Try
updatecmd.Connection.Open()
Dim i = updatecmd.ExecuteNonQuery()
If i = 0 Then
'no rows were updated because none matched the criteria
End If
Catch ex As Exception
'Something went wrong, such as the database was unavailable
End Try
Your error handling should go around the Connection.Open call:
Try
updatecmd.Connection.Open()
Dim i = updatecmd.ExecuteNonQuery()
If i = 0 Then
'no rows were updated because none matched the criteria
End If
Catch ex As Exception
'Something went wrong, such as the database was unavailable
End Try
Hi Mike
Thx. and great HP u got, the access ex. is from ur site.
Ok so if i understand u 100% then do it like this.
Dim strSQL As String = ""
strSQL = "" & _
"UPDATE news_users " & _
"SET newsuser_status = @newsuser_status " & _
"WHERE newsuser_mail = @newsuser_mail " & _
"AND newsuser_pass = @newsuser_pass"
Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MasterConnStr").ConnectionString)
Using updatecmd As OleDbCommand = New OleDbCommand(strSQL, connection)
updatecmd.CommandType = CommandType.Text
updatecmd.Parameters.AddWithValue("@newsuser_status", 1)
updatecmd.Parameters.AddWithValue("@newsuser_mail", TextMail)
updatecmd.Parameters.AddWithValue("@newsuser_pass", TextPass)
Try
updatecmd.Connection.Open()
Dim i = updatecmd.ExecuteNonQuery()
If i = 0 Then
'no rows were updated because none matched the criteria
End If
Catch ex As Exception
'Something went wrong, such as the database was unavailable
End Try
End Using
End Using
If i can ask u another question...
If i use a select statsment, then i have read that i need to call the data after the select like this..
Dim strSQL As String = ""
Dim intStatus As Integer = 0
Dim blnNoRecords As Boolean = True
strSQL = "" & _
"SELECT newsuser_status " & _
"FROM news_users " & _
"WHERE (newsuser_mail = '" & Session("getemail") & "');"
Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MasterConnStr").ConnectionString)
Using command As OleDbCommand = New OleDbCommand(strSQL, connection)
command.Connection.Open()
Using reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
If reader.HasRows Then
reader.Read()
blnNoRecords = False
intStatus = CInt(reader("newsuser_status"))
End If
End Using
End Using
End Using
'Calling the right Multiview View
If blnNoRecords Then
'Mail is not in the DB, added it to the db
MultiView1.SetActiveView(View3)
ElseIf intStatus = 0 Then
'Mail is not activ, do u want to activate it
MultiView1.SetActiveView(View2)
Else
'Mail is activ, do u want to delete it
MultiView1.SetActiveView(View1)
End If
But how to i do this after the call/end using if i want to use Loop or Next, if showing more then one record !? Or do i use the aboved code for ONE Record and this for >1 record !?
And how do i use the Error handler for the to select statsment, hope u understand me...
Dim strSQL As String = ""
strSQL = "" & _
"SELECT * " & _
"FROM news_users"
Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MasterConnStr").ConnectionString)
Using command As OleDbCommand = New OleDbCommand(strSQL, connection)
command.Connection.Open()
Using reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
If reader.HasRows Then
Do While reader.Read()
Response.Write(reader("test_cat") & " " & reader("test_info") & " using reader<br />")
Loop
Else
Response.Write("Ingen data i db")
End If
End Using
End Using
End Using
But how to i do this after the call/end using if i want to use Loop or Next, if showing more then one record !?
You should use while(reader.Read()){……}
siraero
And how do i use the Error handler for the to select statsment, hope u understand me...
Just do as what you did above:
Dim strSQL As String = ""
strSQL = "" & _
"SELECT * " & _
"FROM news_users"
Try
{
Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MasterConnStr").ConnectionString)
Using command As OleDbCommand = New OleDbCommand(strSQL, connection)
command.Connection.Open()
Using reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
If reader.HasRows Then
Do While reader.Read()
Response.Write(reader("test_cat") & " " & reader("test_info") & " using reader<br />")
Loop
Else
Response.Write("Ingen data i db")
End If
End Using
End Using
End Using
}
Catch (Exception exp)
{
………………
}
siraero
Member
419 Points
604 Posts
Where do i place code that show DB error, for OleDbCommand
May 12, 2012 06:32 PM|LINK
Hi
I have this code
Dim strSQL As String = "" strSQL = "" & _ "UPDATE news_users " & _ "SET newsuser_status = @newsuser_status " & _ "WHERE newsuser_mail = @newsuser_mail " & _ "AND newsuser_pass = @newsuser_pass" Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MasterConnStr").ConnectionString) Using updatecmd As OleDbCommand = New OleDbCommand(strSQL, connection) updatecmd.CommandType = CommandType.Text updatecmd.Parameters.AddWithValue("@newsuser_status", 1) updatecmd.Parameters.AddWithValue("@newsuser_mail", TextMail) updatecmd.Parameters.AddWithValue("@newsuser_pass", TextPass) updatecmd.Connection.Open() updatecmd.ExecuteNonQuery() End Using End UsingNow i will add an error code handler to it, so i can response.write an error if i get an error
I have looked at this code, but i dont know if thats the best to use and then if so, where du i place it !?
Dim i As Integer = CInt(updatecmd.ExecuteNonQuery()) If i = 0 Then ' here you can give the user mesage that wrong ...... End IfHope u can help me.
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Where do i place code that show DB error, for OleDbCommand
May 12, 2012 08:35 PM|LINK
Your error handling should go around the Connection.Open call:
Try updatecmd.Connection.Open() Dim i = updatecmd.ExecuteNonQuery() If i = 0 Then 'no rows were updated because none matched the criteria End If Catch ex As Exception 'Something went wrong, such as the database was unavailable End TryBeginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
siraero
Member
419 Points
604 Posts
Re: Where do i place code that show DB error, for OleDbCommand
May 12, 2012 10:07 PM|LINK
Hi Mike
Thx. and great HP u got, the access ex. is from ur site.
Ok so if i understand u 100% then do it like this.
Dim strSQL As String = "" strSQL = "" & _ "UPDATE news_users " & _ "SET newsuser_status = @newsuser_status " & _ "WHERE newsuser_mail = @newsuser_mail " & _ "AND newsuser_pass = @newsuser_pass" Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MasterConnStr").ConnectionString) Using updatecmd As OleDbCommand = New OleDbCommand(strSQL, connection) updatecmd.CommandType = CommandType.Text updatecmd.Parameters.AddWithValue("@newsuser_status", 1) updatecmd.Parameters.AddWithValue("@newsuser_mail", TextMail) updatecmd.Parameters.AddWithValue("@newsuser_pass", TextPass) Try updatecmd.Connection.Open() Dim i = updatecmd.ExecuteNonQuery() If i = 0 Then 'no rows were updated because none matched the criteria End If Catch ex As Exception 'Something went wrong, such as the database was unavailable End Try End Using End UsingIf i can ask u another question...
If i use a select statsment, then i have read that i need to call the data after the select like this..
Dim strSQL As String = "" Dim intStatus As Integer = 0 Dim blnNoRecords As Boolean = True strSQL = "" & _ "SELECT newsuser_status " & _ "FROM news_users " & _ "WHERE (newsuser_mail = '" & Session("getemail") & "');" Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MasterConnStr").ConnectionString) Using command As OleDbCommand = New OleDbCommand(strSQL, connection) command.Connection.Open() Using reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.CloseConnection) If reader.HasRows Then reader.Read() blnNoRecords = False intStatus = CInt(reader("newsuser_status")) End If End Using End Using End Using 'Calling the right Multiview View If blnNoRecords Then 'Mail is not in the DB, added it to the db MultiView1.SetActiveView(View3) ElseIf intStatus = 0 Then 'Mail is not activ, do u want to activate it MultiView1.SetActiveView(View2) Else 'Mail is activ, do u want to delete it MultiView1.SetActiveView(View1) End IfBut how to i do this after the call/end using if i want to use Loop or Next, if showing more then one record !?
Or do i use the aboved code for ONE Record and this for >1 record !?
And how do i use the Error handler for the to select statsment, hope u understand me...
Dim strSQL As String = "" strSQL = "" & _ "SELECT * " & _ "FROM news_users" Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MasterConnStr").ConnectionString) Using command As OleDbCommand = New OleDbCommand(strSQL, connection) command.Connection.Open() Using reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.CloseConnection) If reader.HasRows Then Do While reader.Read() Response.Write(reader("test_cat") & " " & reader("test_info") & " using reader<br />") Loop Else Response.Write("Ingen data i db") End If End Using End Using End UsingDecker Dong ...
All-Star
118619 Points
18779 Posts
Re: Where do i place code that show DB error, for OleDbCommand
May 14, 2012 01:51 AM|LINK
You should use while(reader.Read()){……}
Just do as what you did above:
Dim strSQL As String = "" strSQL = "" & _ "SELECT * " & _ "FROM news_users" Try { Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MasterConnStr").ConnectionString) Using command As OleDbCommand = New OleDbCommand(strSQL, connection) command.Connection.Open() Using reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.CloseConnection) If reader.HasRows Then Do While reader.Read() Response.Write(reader("test_cat") & " " & reader("test_info") & " using reader<br />") Loop Else Response.Write("Ingen data i db") End If End Using End Using End Using } Catch (Exception exp) { ……………… }