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 :)

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

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

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 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!

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 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!

Thank you very much.
I will say, vb.net is a pain for some things.
Have a good one.
Rick

This article has been dead for over six months. Start a new discussion instead.