| | |
sql data pull with no result gives Object reference not set exception
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2005
Posts: 3
Reputation:
Solved Threads: 0
hi all
I get this error no matter what i try, i know this is caused when no data is matched on my query, what i need to know is how to make it so i can give the user an option to register of not.
this is the error:
Line 72: SqlDataAdapter2.Fill(Dsaccess1)
Line 73: dbtaccessRow = Dsaccess1.dbtaccess.FindByrnumber(rnumber)
Line 74: If dbtaccessRow.IsUsernameNull = True Then
Line 75: Else
Line 76: username = dbtaccessRow.Username
Source File: c:\inetpub\wwwroot\DeviceList\ChkAccess.aspx.vb Line: 74
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
DeviceList.ChkAccess.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\DeviceList\ChkAccess.aspx.vb:74
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750
This is my code, very simple:
User1.Text = User.Identity.Name.Split("\", 2)(1)
user1.text =
Dim rnumber As Integer
Dim dbtaccessRow As Dsaccess.dbtaccessRow
SqlDataAdapter2.SelectCommand.CommandText = _
"select username, dbase, aliasname,mgr from dbtaccess where (username = '" & User1.Text & "') and (dbase = '" & Webdbase & "')"
SqlDataAdapter2.Fill(Dsaccess1)
dbtaccessRow = Dsaccess1.dbtaccess.FindByrnumber(rnumber)
'--
If dbtaccessRow.IsUsernameNull = True Then
'Crashes right here each time. becasue user name in the sql-dB does not match the user1.text
' if i change thr user name in teh db, everything work perfect.
'
'--
Else
username = dbtaccessRow.Username
End If
If AliasName = dbtaccessRow.AliasName.Empty Then
AliasName = User1.Text
Else
username = dbtaccessRow.AliasName
End If
Response.Write(username)
If username = User1.Text Then
User1.Text = "Hello:" + dbtaccessRow.AliasName
passed.Text = "You Have Edit Rights to this record, remember to click submit when done editing."
Server.Transfer("addedit.aspx?recordnumber=" & RecordNumber & "")
Else
User1.Text = "Hello:" + AliasName 'user1.Text
Submitrecord.Disabled = True
DeleteRecord.Disabled = True
passed.Text = "Sorry - You currently do NOT have edit ability.
End If
all i am doing is trying to match on user.identity toa sql-db and if it is not there then show the screen with the submit and delete commands grayed out.
I hope some one can help me.
Thanks all
Rick
I get this error no matter what i try, i know this is caused when no data is matched on my query, what i need to know is how to make it so i can give the user an option to register of not.
this is the error:
Line 72: SqlDataAdapter2.Fill(Dsaccess1)
Line 73: dbtaccessRow = Dsaccess1.dbtaccess.FindByrnumber(rnumber)
Line 74: If dbtaccessRow.IsUsernameNull = True Then
Line 75: Else
Line 76: username = dbtaccessRow.Username
Source File: c:\inetpub\wwwroot\DeviceList\ChkAccess.aspx.vb Line: 74
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
DeviceList.ChkAccess.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\DeviceList\ChkAccess.aspx.vb:74
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750
This is my code, very simple:
User1.Text = User.Identity.Name.Split("\", 2)(1)
user1.text =
Dim rnumber As Integer
Dim dbtaccessRow As Dsaccess.dbtaccessRow
SqlDataAdapter2.SelectCommand.CommandText = _
"select username, dbase, aliasname,mgr from dbtaccess where (username = '" & User1.Text & "') and (dbase = '" & Webdbase & "')"
SqlDataAdapter2.Fill(Dsaccess1)
dbtaccessRow = Dsaccess1.dbtaccess.FindByrnumber(rnumber)
'--
If dbtaccessRow.IsUsernameNull = True Then
'Crashes right here each time. becasue user name in the sql-dB does not match the user1.text
' if i change thr user name in teh db, everything work perfect.
'
'--
Else
username = dbtaccessRow.Username
End If
If AliasName = dbtaccessRow.AliasName.Empty Then
AliasName = User1.Text
Else
username = dbtaccessRow.AliasName
End If
Response.Write(username)
If username = User1.Text Then
User1.Text = "Hello:" + dbtaccessRow.AliasName
passed.Text = "You Have Edit Rights to this record, remember to click submit when done editing."
Server.Transfer("addedit.aspx?recordnumber=" & RecordNumber & "")
Else
User1.Text = "Hello:" + AliasName 'user1.Text
Submitrecord.Disabled = True
DeleteRecord.Disabled = True
passed.Text = "Sorry - You currently do NOT have edit ability.
End If
all i am doing is trying to match on user.identity toa sql-db and if it is not there then show the screen with the submit and delete commands grayed out.
I hope some one can help me.
Thanks all
Rick
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
VB.NET Syntax (Toggle Plain Text)
dbtaccessRow = Dsaccess1.dbtaccess.FindByrnumber(rnumber) If dbtaccessRow.IsUsernameNull = True Then
dbtaccessRow is null if there are no records matching in the db so before in between those two lines you need to check if dbtaccessRow is null
•
•
Join Date: Dec 2005
Posts: 3
Reputation:
Solved Threads: 0
Well actually the statement only works if the field does have a NULL, if teh field does not match at all or not Null then if causes teh exception. What you have to do is tell SQL to allow nulls prior to the
dbtaccessRow = Dsaccess1.dbtaccess.FindByrnumber(rnumber)
statement. i found out by trial and error.
you add Dsaccess1.dbtaccess.UsernameColumn.allowNull = True
In stead of doing this: If dbtaccessRow.IsUsernameNull = True Then you can do this and it works better.
if dsccess1.dbtaccess.count -1 then
Thanks for teh response to my question.
Rick
dbtaccessRow = Dsaccess1.dbtaccess.FindByrnumber(rnumber)
statement. i found out by trial and error.
you add Dsaccess1.dbtaccess.UsernameColumn.allowNull = True
In stead of doing this: If dbtaccessRow.IsUsernameNull = True Then you can do this and it works better.
if dsccess1.dbtaccess.count -1 then
Thanks for teh response to my question.
Rick
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
but you have changed the requirements to get round a problem in your code rather than changing your code to meet the requirements - bad practice. I strongly suggest that no system would allow usernames to be null but you have just allowed that. you are correct in doing a rowcount (which you were not doing before). But that should be all you need. Remove the
Good programming practice!
Dsaccess1.dbtaccess.UsernameColumn.allowNull = True and it should still work and you have not changed the requirements to get round a code problem but changed a code problem and still met the requirements
Good programming practice! •
•
Join Date: Dec 2005
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by f1 fan
but you have changed the requirements to get round a problem in your code rather than changing your code to meet the requirements - bad practice. I strongly suggest that no system would allow usernames to be null but you have just allowed that. you are correct in doing a rowcount (which you were not doing before). But that should be all you need. Remove theDsaccess1.dbtaccess.UsernameColumn.allowNull = Trueand it should still work and you have not changed the requirements to get round a code problem but changed a code problem and still met the requirementsGood programming practice!
Thank you very much.
I will say, vb.net is a pain for some things.
Have a good one.
Rick
![]() |
Similar Threads
- how can i extract data from .xml file? (Visual Basic 4 / 5 / 6)
- A function which will transfer the data from a file to STL( vector or map) object. (C++)
- Trying to learn Tkinter. Want help with the menu: (Python)
- problem in installing jdbc... (Java)
- ASP and SQL ? (MS SQL)
Other Threads in the VB.NET Forum
- Previous Thread: Help with game server, client
- Next Thread: Newbie to .Net 2003
| Thread Tools | Search this Thread |
"crystal .net .net2005 .net2008 2008 access add advanced application array assignment basic beginner box browser button buttons click code combo convert cpu cuesent data database datagrid datagridview datetimepicker designer dissertation dissertations dissertationthesis dissertationtopic dosconsolevb.net editvb.net employees excel exists firewall forms html image images isnumericfuntioncall listview login map math memory mobile module mssqlbackend mysql navigate net number opacity open pdf picturebox2 port print printpreview record regex reports" reuse right-to-left save savedialog search serial settings socket sqldatbase sqlserver storedprocedure string temp textbox timer timespan transparency txttoxmlconverter useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vba vbnet vista visual visualbasic.net visualstudio.net web wpf wrapingcode xml





