473,387 Members | 1,899 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,387 software developers and data experts.

Object reference not set to an instance of an object.

my que is i have used this code for retriving data from grid view and insert it into database.now at the button event the values are not retrived from the gridview.it gives error Object reference not set to an instance of an object.wht is soln?

Expand|Select|Wrap|Line Numbers
  1. public partial class frmshowcustapp : System.Web.UI.Page
  2. {
  3.     public SqlCommand cmd;
  4.     public string str;
  5.     public SqlDataAdapter dtp;
  6.     public DataSet ds;
  7.     public SqlDataReader dr;
  8.     public string id, birthday, c_name,phone_no, address, l_or_pno, emailid, city, pincode, occuption,photo,imagpath,dealer_list, sex, f_member;
  9.     //"id","c_name","birthday","phone_no","address","l_or_pno","emailid","city","pincode""occuptiopn","sex","f_member");
  10.   //  public partial class migrated_frmshowcustapp:frmshowcustapp
  11.  
  12.  
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         if (!IsPostBack)
  16.         connection.getconn();
  17.         //Panel1.Visible = false;
  18.     }
  19.     protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  20.     {
  21.         //Panel1.Visible = true;
  22.  
  23.         id = GridView1.SelectedRow.Cells[1].Text;
  24.  
  25.         c_name = GridView1.SelectedRow.Cells[2].Text;
  26.         birthday = GridView1.SelectedRow.Cells[3].Text;
  27.         phone_no = GridView1.SelectedRow.Cells[4].Text;
  28.         address = GridView1.SelectedRow.Cells[5].Text;
  29.         photo = GridView1.SelectedRow.Cells[15].Text;
  30.         imagpath = GridView1.SelectedRow.Cells[6].Text;
  31.         l_or_pno = GridView1.SelectedRow.Cells[7].Text;
  32.         emailid = GridView1.SelectedRow.Cells[8].Text;
  33.         city = GridView1.SelectedRow.Cells[9].Text;
  34.         pincode = GridView1.SelectedRow.Cells[10].Text;
  35.         occuption = GridView1.SelectedRow.Cells[13].Text;
  36.         sex = GridView1.SelectedRow.Cells[11].Text;
  37.         f_member = GridView1.SelectedRow.Cells[12].Text;
  38.         dealer_list = GridView1.SelectedRow.Cells[14].Text;
  39.         txtemailid.Text = GridView1.SelectedRow.Cells[8].Text;
  40.         txtcname.Text = GridView1.SelectedRow.Cells[2].Text;
  41.  
  42.         }
  43.  
  44.     protected void btnsavec_Click(object sender,EventArgs e)
  45.     {
  46.  
  47.         id = GridView1.SelectedRow.Cells[1].Text;
  48.  
  49.         c_name = GridView1.SelectedRow.Cells[2].Text;
  50.         birthday = GridView1.SelectedRow.Cells[3].Text;
  51.         phone_no = GridView1.SelectedRow.Cells[4].Text;
  52.         address = GridView1.SelectedRow.Cells[5].Text;
  53.         photo = GridView1.SelectedRow.Cells[15].Text;
  54.         imagpath = GridView1.SelectedRow.Cells[6].Text;
  55.         l_or_pno = GridView1.SelectedRow.Cells[7].Text;
  56.         emailid = GridView1.SelectedRow.Cells[8].Text;
  57.         city = GridView1.SelectedRow.Cells[9].Text;
  58.         pincode = GridView1.SelectedRow.Cells[10].Text;
  59.         occuption = GridView1.SelectedRow.Cells[13].Text;
  60.         sex = GridView1.SelectedRow.Cells[11].Text;
  61.         f_member = GridView1.SelectedRow.Cells[12].Text;
  62.         dealer_list = GridView1.SelectedRow.Cells[14].Text;
  63.         txtemailid.Text = GridView1.SelectedRow.Cells[8].Text;
  64.         txtcname.Text = GridView1.SelectedRow.Cells[2].Text;
  65.  
  66.  
  67.        str="insert into customer_list values('" + id +"','"+ c_name +"','" + birthday +"','"+phone_no+"','"+address+"','"+photo+"','"+imagpath+"','"+l_or_pno+"','"+emailid+"','"+city+"','"+pincode+"','"+occuption+"','"+sex+"','"+f_member+"','"+dealer_list+"','"+txtid.Text+"')";
  68.         cmd = new SqlCommand(str,connection.conn);
  69.         cmd.ExecuteNonQuery();
  70.         lblmsg.Text = "done";
  71.  
  72.     }
Feb 3 '12 #1
2 1128
Rabbit
12,516 Expert Mod 8TB
I don't see connection defined anywhere.
Feb 3 '12 #2
whenever u r accessing any object, make sure that object is not null. For this u always can check with conditional statement and write ur executing that code if object not null condition is true.
Mar 5 '12 #3

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: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.