Click here to Skip to main content
14,934,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in vb class file After every 30s i checked in database . if i get value from database then only i redirect page.
but issue at redirecting page , when i trying to redirect, i got error as 'Object Reference Not set to an instance of an object'
Please help me with this query.
Thank u in advance

What I have tried:

Public Sub Start_Timer()
Dim tym As New System.Timers.Timer
tym.Interval = 20000
tym.Start()
AddHandler tym.Elapsed, New ElapsedEventHandler(AddressOf tick)
End Sub
Public Sub tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
Try
Dim Conn As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=VMS_DB;Integrated Security=true")
Dim tab As DataTable = New DataTable
Dim query As String
Conn.Open()
query = "select Top 1 * from Vehicle_Record where Flag=1 order by Flag asc"
Dim sda As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(query, Conn)
sda.Fill(tab)
If tab.Rows.Count <> 0 Then
Dim vehicle_No As String = tab.Rows(0).Item("Vehicle_No")
Dim str As String = "Vehicle_Acceptance_Form.aspx?VehicleNo=" + vehicle_No + ""
System.Web.HttpContext.Current.Response.Redirect("Vehicle_Acceptance_Form.aspx?VehicleNo=" + vehicle_No)
Dim update_Query As String = "Update Vehicle_Record set Flag=0 where Vehicle_No='" & vehicle_No & "' and Flag=1"
Dim da As SqlClient.SqlCommand = New SqlClient.SqlCommand(update_Query, Conn)
da.ExecuteNonQuery()
Conn.Close()
Else
Conn.Close()
End If
Catch ex As Exception

End Try

End Sub
Posted
Updated 5-May-16 6:24am
Comments
Tejaswi Deshmukh 5-May-16 8:16am
   
System.Web.HttpContext.Current.Response.Redirect("Vehicle_Acceptance_Form.aspx?VehicleNo=" + vehicle_No) on this line error occur
F-ES Sitecore 5-May-16 10:01am
   
You need to learn to debug your code. Put a breakpoint on that line and see what you're referencing (ie have a "." after) that is null. Is System null? System.Web? System.WebHttoContent.Current? etc, go down the chain and see exactly what you are referencing that is null.

1 solution

You cannot do it like this. The timer class cannot be used in ASP.Net like this. Why? Because the web does not work that way. You VB code sits doing nothing on the server until someone types in your url into their browser. Then the server takes the request, executes the code in the page, and then sends a response back to the client and then disconnects. So, if you kicked off a timer during that process when the timer fires there is no connection which is why .Current is null, you aren't in a current page request. It's long gone.

This is a huge difference between windows apps and web apps. If you want to redirect after some time you'll need to do it from the client side. For example, you can kick off a timer in JavaScript using setTimeout() that then checks for a response and does something depending on what you want.

You need to rethink what it is you are trying to accomplish.
   
Comments
Tejaswi Deshmukh 6-May-16 7:21am
   
Can you please give me example for that??
ZurdoDev 6-May-16 7:30am
   
I don't exactly understand what you want, so not really.

But look up setTimeout and also "how to redirect to new url in javascript" and you should be able to get started.
Tejaswi Deshmukh 6-May-16 7:48am
   
i want to check database after every 30 s. if i will get value from Database than only i redirect page on given url else there is no redirecting of page.
ZurdoDev 6-May-16 8:03am
   
1. You'll need a webservice that you can call from javascript. Your webservice will query your database and then return an appropriate value. Google c# webservice and there are tons of examples.
2. Use jQuery's $.ajax(), http://api.jquery.com/jQuery.ajax/, to call the webservice. Very easy to use and tons of examples online.
3. In the success function of your .ajax() call you then will either reset the timer for another 30 seconds (or whatever you want) or redirect or give an error, whatever you want.

That's the basic steps that you need.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900