In codebehind I have the following:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
Dim sql As String
Dim conn As OleDbConnection
Dim cmd As OleDbDataAdapter
Dim ds As DataSet
Dim tbl As DataTable
conn = New OleDbConnection("Provider=SQLOLEDB;Data Source=(local);Initial Catalog='library';Integrated Security=SSPI;")
Try
sql = "SELECT f1, f2, f3 FROM mydb WHERE ltrim(rtrim(UPPER(username))) = 'MYNAME'"
cmd = New OleDbDataAdapter(sql, conn)
ds = New DataSet
cmd.Fill(ds)
tbl = New DataTable
tbl = ds.Tables(0)
If tbl.Rows.Count = 0 Then
Response.Redirect("goback.html")
End If
Response.Redirect("dummy.html")
Catch ex As Exception
Response.Redirect("goback2.html")
End Try
End Sub
The routine works to a point. I can check the value of tbl.Rows.Count = 1, so it should redirect to "dummy.html" WHICH IT DOES CORRECTLY so long as I comment out the line that redirects to "goback2.html".
If I uncomment the line to redirect to goback2, then it goes to "goback2.html"
I thought it should only execute that code if there was some error in the previous code - but there can't be an error in the previous code, if it executes. It's like it's executing the catch code regardless of what I'm going.
Oddly, it ONLY messes up with the redirect! If I replace the redirect to goback2 with an assignment to a textbox.text then it works (by ignoring that code) - but the redirect it seems to execute regardless of whether it should take the catch
Response.Redirect(string) throws a ThreadAbortException when Response.End() is called.
Use the overload that takes a string and a boolean instead:
Response.Redirect("goback.html", false);
From MSDN: the second parameter, endResponse, "Indicates whether execution of the current page should terminate."
Response.Redirect throws a ThreadAbortException to terminate the current request.
Your Catch block is catching that exception.
It may be because of ThreadAbortedException because of Response.Redirect. - MSDN support link
You haven't open the connection. You cannot redirect in an Try/Catch. You should set a boolean variable success to false and check for it after the Try/Catch/Finally and redirect if it's set to false.
Have a look at this SO-Question: Is there something that prevents Response.Redirect to work inside try-catch block?
This would be better:
Dim success As Boolean = True
Dim ds As New DataSet
Using conn As New OleDb.OleDbConnection("Provider=SQLOLEDB;Data Source=(local);Initial Catalog='library';Integrated Security=SSPI;")
Try
Dim Sql = "SELECT f1, f2, f3 FROM mydb WHERE ltrim(rtrim(UPPER(username))) = 'MYNAME'"
Dim cmd = New OleDb.OleDbDataAdapter(Sql, conn)
conn.Open()
cmd.Fill(ds)
Catch ex As Exception
success = False
End Try
End Using
If Not success Then
Response.Redirect("goback2.html")
ElseIf ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 0 Then
Response.Redirect("goback.html")
Else
Response.Redirect("dummy.html")
End If
Related
My new application page is getting a timeout error every hour or so after getting some traffic on the page, and by traffic I mean users submitting 5-10 applications. How do I find the cause of the connections getting tied up?
This has been an issue in the past so whenever I use a sql data reader object I make sure to implement the "Using" statement. I've also made sure that the thread isn't aborted before the data reader is disposed of. I doubt that my use of data readers is the issue, so maybe it's my non-query code that's causing the issue, but I can't see why.
I also use a few sqldatasource objects for my dropdownlist controls, and as far as I know it wouldn't be the source of my issue.
See code example for how I use my sql objects.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Using drApp As SqlDataReader = LookupAppByID()
'some code
drApp.Close()
End Using
End Sub
Public Function LookupAppByID() As SqlDataReader
Dim Command As New SqlClient.SqlCommand
Command.Connection = GetDBConnection()
Command.CommandType = CommandType.Text
Command.CommandText = "select statement"
Return Command.ExecuteReader(CommandBehavior.CloseConnection)
End Function
Public Function UpdateAppStatus() As Integer
UpdateAppStatus = 0
Using Command As New SqlClient.SqlCommand("update statement", GetDBConnection())
UpdateAppStatus = Command.ExecuteNonQuery()
Command.Connection.Close()
Command.Connection.Dispose()
Command.Dispose()
End Using
End Function
Public Function GetDBConnection() As SqlClient.SqlConnection
Dim connection As New SqlClient.SqlConnection
connection.ConnectionString = "connection string"
connection.Open()
Return connection
End Function
Obviously I expect it to chug along without a hitch but when users start hitting the page it gets this error: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
Is there a problem with my code?
How can I narrow down what is causing this issue?
I would keep the database objects local so I can ensure that they are closed and disposed. Create and dispose within one method. I created a class so you can easily pass all its properties in a single variable.
'User Interface Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim id As Integer 'No sure where the value comes from
Using dtApp As DataTable = DataAccess.LookupAppByID(id)
'some code
End Using
End Sub
Protected Sub Update_Click(sender As Object, e As EventArgs) Handles Update.Click
Dim Applic As New ApplicationData()
Applic.ApplicantsName = txtName.Text
Applic.ID = CInt(txtID.Tex)
Applic.ApplicationStatus = "Approved"
Dim retVal = DataAccess.UpdateAppStatus(Applic)
If retVal = 1 Then
'Alert user of success
Else
'Alert user of failure
End If
End Sub
Public Class ApplicationData
Public Property ID As Integer
Public Property AppDate As Date
Public Property ApplicantsName As String
Public Property ApplicationStatus As String
End Class
Public Class DataAccess
Private Shared ConString As String = "Your connection string"
Public Shared Function LookupAppByID(AppID As Integer) As DataTable
Dim dt As New DataTable
Using cn As New SqlConnection(ConString)
Using Command As New SqlCommand("select * From YourTable Where ID = #ID", cn)
Command.Parameters.Add("#ID", SqlDbType.Int).Value = AppID
cn.Open()
dt.Load(Command.ExecuteReader)
End Using
End Using
Return dt
End Function
Public Shared Function UpdateAppStatus(App As ApplicationData) As Integer
Dim AppStatus = 0
Using cn As New SqlConnection(ConString)
Using Command As New SqlClient.SqlCommand("Update YourTable Set Status = #Status Where ID = #ID;", cn)
Command.Parameters.Add("#Status", SqlDbType.VarChar, 50).Value = App.ApplicationStatus
Command.Parameters.Add("#ID", SqlDbType.Int).Value = App.ID
cn.Open()
AppStatus = Command.ExecuteNonQuery()
End Using
End Using
Return AppStatus
End Function
End Class
I have a page where a user fills in some text boxes which get saved to a SQL database using a Submit button. The page also contains a button that allows them to upload attachments. If the user uploads an attachment BEFORE clicking the submit button to save the other data, the values in the text boxes are cleared once the upload routine executes the Response.Redirect(Request.Url.AbsoluteUri). I have tried saving the values I want to restore into the Session, but I don't seem to be able to restore them. The debugger shows they are there, but once the Response.Redirect is executed, the next lines are never executed. I'm brand new to ASP.NET, so I may just be missing something obvious. Here is the code for the upload procedure:
Protected Sub Upload(sender As Object, e As EventArgs) Handles btnUpload.Click
Session("Phone") = txtPhone.Text
Session("Name") = txtName.Text
Session("Email") = txtEmail.Text
Session("StartDate") = txtStartDate.Text
Session("EndDate") = txtEndDate.Text
Session("Subject") = txtSubject.Text
Session("Description") = txtDescription.Value
Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim contentType As String = FileUpload1.PostedFile.ContentType
Using fs As Stream = FileUpload1.PostedFile.InputStream
Using br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(fs.Length)
Dim constr As String = ConfigurationManager.ConnectionStrings("EngineeringRequestsConnectionString").ConnectionString
Using con As New SqlConnection(constr)
Dim query As String = "insert into Attachments values (#id, #Name, #ContentType, #Data)"
Using cmd As New SqlCommand(query)
cmd.Connection = con
cmd.Parameters.Add("#id", SqlDbType.Int).Value = nextId
cmd.Parameters.Add("#Name", SqlDbType.VarChar).Value = filename
cmd.Parameters.Add("#ContentType", SqlDbType.NVarChar).Value = contentType
cmd.Parameters.Add("#Data", SqlDbType.VarBinary).Value = bytes
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Using
End Using
hasUpload = True
Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri)
BindGrid()
End Sub
The BindGrid() procedure attempts to restore the values from Session but never gets executed.
If hasUpload Then
txtPhone.Text = CType(Session("Phone"), String)
txtName.Text = CType(Session("Name"), String)
txtStartDate.Text = CType(Session("StartDate"), String)
End If
This is my first post on SO. I apologize if in advance if it is not clear enough.
If you are new to ASP.NET webforms it's probably worth checking out the Page Lifecycle as this dictates the order in which events are fired when a page is loaded. The issue is that you are effectively taking the user from page A to page B but expecting them to see results on page A.
In your method
Protected Sub Upload(sender As Object, e As EventArgs) Handles btnUpload.Click
.. skip ..
Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri)
BindGrid()
When you call Response.Redirect() the browser will redirect to a new page (e.g. from A -> B), this will start the page lifecycle over again, anything that happens after Response.Redirect() won't be rendered. I think what is confusing you is that you are redirecting from (A -> A), however this will still cause the page to be reloaded.
One option is to call BindGrid() and reload the data from session in one of the page load events, or remove the call to Response.Redirect() all together and instead leave the page as-is.
I am having an issue with my login page. I am not getting any errors so am not able to know where the problem is?
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login.Click
'connection string
Dim mysqlconn As MySqlConnection = New MySqlConnection("server=localhost;user id=root;Password=123;database=users;persist security info=False")
Dim cmd As New MySqlCommand
Dim da As New MySqlDataAdapter
Dim mydata As New DataTable
Dim reader As MySqlDataReader
Try
mysqlconn.Open()
Dim query As String
query = "SELECT * FROM login_form where Username = '" & rfvUser.Text & "' and Password='" & rfvPWD.Text & "'"
cmd = New MySqlCommand(query, mysqlconn)
reader = cmd.ExecuteReader
While reader.Read()
If rfvUser.Text = "admin" And rfvPWD.Text = "admin" Then
Me.Session("User") = Me.rfvUser.Text
Server.Transfer("Admin.aspx")
ElseIf (rfvUser.Text = reader("UserName").ToString()) And (rfvPWD.Text = reader("Password").ToString()) Then
Me.Session("User") = Me.rfvUser.Text
Server.Transfer("Ersal_send.aspx")
Else
ClientScript.RegisterStartupScript(Page.[GetType](), "validation", "<script language='javascript'>alert('Invalid Username or Password')</script>")
reader.Close()
End If
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
mysqlconn.Dispose()
End Try
End Sub
End Class
Have you tried running the query directly via a SQL client? If your query is not returning any rows, then your procedure will simply exit without any errors as it will never enter the While loop.
Another advice: It is never a good idea to pass user input directly into a query. This leads to SQL injection. Use parameterised queries. Google for it.
I am making Log in page for my project but I am getting an error "There is no row at position 0" while running.
I tried these lines of codes.
Imports System.Data.SqlClient
Imports System.Data
Partial Class Dept_login
Inherits System.Web.UI.Page
Protected Sub BtnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
Dim ds As New DataSet
'Try
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("VMSConnectionString").ConnectionString)
con.Open()
Dim cmd As New SqlCommand("SELECT password FROM Dept_login WHERE user_id='" + Txtuname.Text + "'", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds)
If Not IsDBNull(ds) Then
If Txtpwd.Text = ds.Tables(0).Rows(0).Item("password") Then
Response.Redirect("Online Services.aspx") 'the page i want to redirect to after login successful
Else
Label1.Visible = True 'initially visible is false and text is INVALID PASSWORD
End If
con.Close()
' Catch ex As Exception
' End Try
End If
End Sub
Private Function Dept_login() As Integer
Throw New NotImplementedException
End Function
End Class
This line doesn't make sense:
If Not IsDBNull(ds) Then
ds will never be DBNull. Instead, check for the number of rows coming back, like:
If ds.Tables(0).Rows.Length > 0 Then
You're trying to get the first row (.Rows(0)) when there aren't any - that's what the error is telling you.
Try using something like this:
If ds.Tables(0).Rows.Count > 0 AndAlso Txtpwd.Text = ds.Tables(0).Rows(0).Item("password") Then
Response.Redirect("Online Services.aspx", False) 'the page i want to redirect to after login successful
Context.ApplicationInstance.CompleteRequest();
Else
Label1.Visible = True 'initially visible is false and text is INVALID PASSWORD
End If
con.Close()
' Catch ex As Exception
' End Try
(Note: You should use parameterization for the SQL query. You're leaving yourself open to a SQL injection attack.)
This line doesn't make sense:
If Not IsDBNull(ds) Then
Makes sense
If ds.Tables(0).Rows.count > 0 andalso ds.Tables..count > 0 Then
END IF
Hope this helps
Did you try using a datareader instead of a dataadapter?
Try
Dim datare As SqlDataReader
Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("VMSConnectionString").ConnectionString)
Using cmd As New SqlCommand("SELECT password FROM Dept_login WHERE user_id='#User'", cn)
cmd.Parameters.AddWithValue("#User", Txtuname.Text)
cn.Open()
datare = cmd.ExecuteReader()
With datare
If .Read() Then
If .Item(0) = txtpwd.Text Then
Response.Redirect("Online Services.aspx")
Else
Label1.Visible = True
End If
End If
End With
End Using
End Using
Catch ex As Exception
Throw ex
End Try
Also, on the query you write user_id. did you mean username? Is you query correct?
We have a stored proc that grabs that data from a table, let's call it table1 from one db1 and populates a table, called table2 in db2.
We scheduled this stored proc to perform this function like every 5 minutes. This works fine.
Given that users mostly leave their apps while performing other administrative functions, we have created a refresh button that refreshes the GridView upon clicking the button.
Protected Sub Refresh_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Refresh.Click
' Clean up text to return the GridView to it's default state
strSearch.Text = ""
SearchString = ""
gridView1.DataBind()
End Sub
We would like to both click this refresh button to both to have the stored procedure update table2 and at same time, refresh the GridView.
I know that gridview1.Databind() refreshes the GridView with latest data from table2 but how do I ensure that table2 is first updated with data from table1 using the stored proc before refreshing the GridView.
Kenny I have prepared complete code in vb for you, take a look:
Private Sub CallRefresh()
Dim connectionString As String = "Initial Catalog=myDataBase;data source=myServer;Integrated Security=SSPI;"
Dim storedProcName As String = "ActivateServerTask"
Dim conn As SqlConnection = New SqlConnection(connectionString)
conn.Open()
Dim command As SqlCommand = New SqlCommand(storedProcName, conn)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add(New SqlParameter("#taskId", 1))
command.ExecuteNonQuery()
command.Dispose()
conn.Close()
conn.Dispose()
End Sub
Protected Sub Refresh_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Refresh.Click
CallRefresh()
gridView1.DataBind()
End Sub
CallRefresh opens connection, executes "ActivateServerTask" stored procedure with "taskId" parameter and finally closes connection...
Take notice I am using connection to sql with integrated security.
You can find other connection strings here:
http://www.connectionstrings.com/
Happy coding Kenny!
Just call your stored procedure from application before you call gridView1.DataBind()
This is how you call a stored procedure from your code:
public object ExecuteStoredProcedureAsValue(SqlConnection conn,
string storedProcName,
List<SqlParameter> parameters)
{
using (var command = new SqlCommand(storedProcName, conn)) {
command.CommandType = CommandType.StoredProcedure;
if (parameters != null)
parameters.ForEach(p => command.Add(param));
object result = null;
using (var reader = command.ExecuteReader()) {
while (reader.Read())
{
result = reader[0];
break;
}
}
return result;
}
}
Take notice that "conn" is SqlConnection object. You should initialize and open sql connection before calling this code.