Showing posts with label textbox. Show all posts
Showing posts with label textbox. Show all posts

Monday, March 26, 2012

Object reference not set to an instance of an object

I am trying to get out a sellerID from the a table based on the seller email address. I am getting this werd error.

Here is my code:
I have a textbox for the email address, and a Label to get the sellerID from the seller Table
And the Submit button I have this code:

GetSellerID(txtemail.Text)
datarad = ds.Tables(0).Rows(0)
lblsellerID.Text = datarad.Item(0)

Note: I have created a function name GetSellerID I also have a store Procedure call SellerID. I have tested this it works fine using query analyzer.

This is function:

Public Function GetSellerID(ByVal Email As String) As DataSet
sql = "exec SellerID " & "Email"
Dim connString As String = "Server=localhost; Initial Catalog=mydatabase; UID=sa; Password=mypwd; Network Library=dbmssocn;"

Dim conn As SqlConnection = New SqlConnection(connString)
'Dim command As SqlCommand = New SqlCommand(sql, conn)

Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn)
Dim ds As DataSet = New DataSet
conn.Open()

Try
'
da.Fill(ds, "Seller")
Return ds
Catch e As SqlException

Throw (e)
Finally

conn.Close()
End Try
End Function

when I click the submit I get this error :Object reference not set to an instance of an object.

Please Help.
Thanks in adavance. I am using visual studio 2003.what is the offending line of code according to the error?
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 99:
Line 100: GetSellerID(txtemail.Text)
Line 101: datarad = ds.Tables(0).Rows(0)
Line 102: lblsellerID.Text = datarad.Item(0)
Line 103:

Source File: d:\inetpub\wwwroot\Computers\Seller.aspx.vb Line: 101

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
Computers.Seller.btnsend_Click(Object sender, EventArgs e) in d:\inetpub\wwwroot\Computers\Seller.aspx.vb:101
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
if i am reading this correctly, GEtSellarID is a function which returns a dataset containing your data.

looking at your code you call GetSellerID and dont catch the dataset return value

in your code .make it the following

dim _ds as dataset = getsellerid(txtemail.text)

_ds.tables(0).rows(0) will now contain your data.
Hi Richy

thanks for the reply. I tried the code:
Dim ds As DataSet = GetSellerID(txtemail.Text)
datarad = ds.Tables(0).Rows(0)
lblsellerID.Text = datarad.Item(0)

When I run the application it states

There is no row at position 0.
Sorry:

I found that was a bit of error in my GetSeller function. it is working fine fine
Thanks a lot for the help

Saturday, March 24, 2012

object reference not set to an instance of an object

Dim Override As TextBox = CType(uxTimeEntry.FindControl("uxOverride"),
TextBox)
Override.Attributes.Add("onblur", "Override();")

why do I get the message like "object reference not set to an instance of an
object"?
how can I fix?It doesn't find the control.

You should 99.99 % of the time check for null (Nothing in vb.net) when you
code.

Dim Override As TextBox = CType(uxTimeEntry.FindControl("uxOverride"),
TextBox)

if (Not (Override Is Nothing)) Then

Override.Attributes.Add("onblur", "Override();")
End If

Or better yet:

if (Not (Override Is Nothing)) Then
Override.Attributes.Add("onblur", "Override();")
else
Throw New ArgumentException("Excected Control was not found")
End If

...

C#

TextBox tb = uxTimeEntry.FindControl("uxOverride") as TextBox;
//or
//TextBox tb = (TextBox)uxTimeEntry.FindControl("uxOverride") ;
if (null!=tb)
{
tb.Attributes.Add("onblur", "Override();");
}

"KenLee" <KenLee@.discussions.microsoft.com> wrote in message
news:FB958839-225C-41AE-8ABF-8FC5C1D81746@.microsoft.com...
> Dim Override As TextBox = CType(uxTimeEntry.FindControl("uxOverride"),
> TextBox)
> Override.Attributes.Add("onblur", "Override();")
> why do I get the message like "object reference not set to an instance of
an
> object"?
> how can I fix?
Kenly
findcontrol returned nothing or uxtimeentry is nothing. Check to make sure
your passing in the correct controlid it might be something like uxOverride1
or the case could be incorrect. Run in debug and display
uxTimeEntry.controls one by one and try the ctype in the immediate window.

Good Luck
DWS

"KenLee" wrote:

> Dim Override As TextBox = CType(uxTimeEntry.FindControl("uxOverride"),
> TextBox)
> Override.Attributes.Add("onblur", "Override();")
> why do I get the message like "object reference not set to an instance of an
> object"?
> how can I fix?

object reference not set to an instance of an object

Dim Override As TextBox = CType(uxTimeEntry.FindControl("uxOverride"),
TextBox)
Override.Attributes.Add("onblur", "Override();")
why do I get the message like "object reference not set to an instance of an
object"?
how can I fix?It doesn't find the control.
You should 99.99 % of the time check for null (Nothing in vb.net) when you
code.
Dim Override As TextBox = CType(uxTimeEntry.FindControl("uxOverride"),
TextBox)
if (Not (Override Is Nothing)) Then
Override.Attributes.Add("onblur", "Override();")
End If
Or better yet:
if (Not (Override Is Nothing)) Then
Override.Attributes.Add("onblur", "Override();")
else
Throw New ArgumentException("Excected Control was not found")
End If
..
C#
TextBox tb = uxTimeEntry.FindControl("uxOverride") as TextBox;
//or
//TextBox tb = (TextBox)uxTimeEntry.FindControl("uxOverride") ;
if (null!=tb)
{
tb.Attributes.Add("onblur", "Override();");
}
"KenLee" <KenLee@.discussions.microsoft.com> wrote in message
news:FB958839-225C-41AE-8ABF-8FC5C1D81746@.microsoft.com...
> Dim Override As TextBox = CType(uxTimeEntry.FindControl("uxOverride"),
> TextBox)
> Override.Attributes.Add("onblur", "Override();")
> why do I get the message like "object reference not set to an instance of
an
> object"?
> how can I fix?
>
Kenly
findcontrol returned nothing or uxtimeentry is nothing. Check to make sure
your passing in the correct controlid it might be something like uxOverride1
or the case could be incorrect. Run in debug and display
uxTimeEntry.controls one by one and try the ctype in the immediate window.
Good Luck
DWS
"KenLee" wrote:

> Dim Override As TextBox = CType(uxTimeEntry.FindControl("uxOverride"),
> TextBox)
> Override.Attributes.Add("onblur", "Override();")
> why do I get the message like "object reference not set to an instance of
an
> object"?
> how can I fix?
>

Thursday, March 22, 2012

Object reference not set to an instance of an object.

i have a dropdownlist and 2 textbox's on my webform.
i have a table 'Areas', with 2 fields in the dataset.

In the Data properties of the DDL i have its DataTextField set to the
field 'areaName', and its DataValueField set to the other field
'areaID'.

When i select an item from the DDL i want the 'areaName' to go into
textBox1 and the 'areaID' to go into textbox2.

when i try get the the selected value:
TextBox1.Text = DropDownList1.SelectedValue
which works, gives me the areaName.

however when i try get the other field, areaID :
TextBox2.Text = DropDownList1.SelectedItem.Text
i get the error :
Object reference not set to an instance of an object.

can anyone tell me what exactly this error means, please?may be u hav same areaid for more than one records.
chek select statement.