473,378 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Object Reference not set to an instance

Hi Team,

Am using Grid View in ASP.Net while am trying to update the row it was not getting updated. Instead, it throw an exception Object reference not set to an instance. please help me in this regard.

Expand|Select|Wrap|Line Numbers
  1. Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
  2.         Try
  3.             Dim txtName, txtName1, txtName2, txtname3 As New TextBox
  4.             Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
  5.             txtName = CType(row.FindControl("txtName"), TextBox)
  6.             txtName1 = CType(row.FindControl("txtName1"), TextBox)
  7.             txtName2 = CType(row.FindControl("txtName2"), TextBox)
  8.             txtname3 = CType(row.FindControl("txtName3"), TextBox)
  9.  
  10.             'doupdate(txtName1, txtName2, txtname3, txtName)
  11.             'con.Open()
  12.             cmd = New MySqlCommand("update login set username='" & txtName1.Text & "',password='" & txtName2.Text & "',Usertype='" & txtname3.Text & "' where id='" & txtName.Text & "')", con)
  13.             da = New MySqlDataAdapter(cmd)
  14.             cmd.ExecuteNonQuery()
  15.             ds = New DataSet
  16.             da.Fill(ds)
  17.             GridView1.DataSource = ds
  18.             GridView1.DataBind()
  19.             con.Close()
  20.         Catch ex As Exception
  21. msgbox(ex.Message.ToString)
  22.         End Try
  23.  
  24.  
  25.     End Sub
Jul 10 '10 #1
3 1422
Which line did it specifically throw the error on? Please debug line by line if you can and let us know. Thanks.
Jul 12 '10 #2
Frinavale
9,735 Expert Mod 8TB
You are retrieving TextBoxes from the row using the FindControl command.
The FindControl method may not have been able to retrieve a TextBox though and so it will return [il]Nothing[/il]. You should always check to make sure that the controls that you are retrieving exist before you attempt to use them.

For example, the following will check to make sure that "txtName" exists:
Expand|Select|Wrap|Line Numbers
  1. If txtName IsNot Nothing Then
  2.   'txtName was successfully retrieved...it is not nothing
  3.   'you can use it without experiencing a Null Reference Exception
  4. Else
  5.   'something went wrong and txtName does not exist 
  6. End If
Are you sure that the IDs of the TextBoxes are correct?

-Frinny

(PS you should be using parameters to create your SQL command...you should not be dynamically creating your SQL statement by concatenating user provided data into your SQL command because this leaves you open to a SQL Injection Attack)
Jul 12 '10 #3
@Frinavale
Thank a lot for your reply. Its working fine now.
Jul 14 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
6
by: NewToDotNet | last post by:
I am getting "Object reference not set to an instance of an object. " when I attempt to open a C# windows service class in design view, although I was able to initially create the service and open...
4
by: Frawls | last post by:
Hi, I get the following error when trying to run a search on my aspx site, this error only occours if the product im searching for does not exist. Can anybody explain this please and help me...
2
by: jw56578 | last post by:
why am i getting the "Object reference not set to an instance of an object" error on all page registeration tags in my project. <%@ Page Language="vb" AutoEventWireup="false"...
3
by: nemo | last post by:
Hi, My application works fine on the localhost but spits this error as soon as I put it on the server. I know this error occurs when an object has not been instantiated prior to a reference, but...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
7
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As...
6
by: kalaivanan | last post by:
hi, i am a beginner in c#. i have theoretical knowledge about object, reference and instance. but i want to know clearly about what is an object, reference and instance. can any one help me? or...
1
by: vishnu | last post by:
Hi, I am working on asp.net project which I converted the code fron VB to C# and instead of RaiseEvent in VB code I used the following code. using System; using System.Data; using...
4
by: livmacca | last post by:
Hi, I am new to VB .Net programming and is trying to create a webpage. I encountered the following error and is totally clueless on how to make it work: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.