473,506 Members | 17,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to fix reference not set to an instance of an object

1 New Member
How do I fix the "reference not set to an instance of an object" error?

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data.SqlClient;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Collections;
  9.  
  10. /// <summary>
  11. /// Summary description for DbWork
  12. /// </summary>
  13. public class DbWork
  14. {
  15.     DataTable Tbl;
  16.     SqlConnection con;
  17.     SqlCommand cmd;
  18.     // dbcall means storedproceudre or sql statement
  19.     // method to initialize con to db
  20.     private void Initialize(CommandType CT, string DbCall)
  21.     {
  22.         con = new SqlConnection();
  23.         con.ConnectionString = ConfigurationManager.ConnectionStrings[1].ToString();
  24.         cmd = new SqlCommand();
  25.         cmd.Connection = con;
  26.         cmd.CommandType = CT;
  27.         cmd.CommandText = DbCall;
  28.         con.Open();
  29.     }
  30.     public int RunProcedure(string SpName, SortedList ParaValu)
  31.     {
  32.  
  33.         Initialize(CommandType.StoredProcedure, SpName);
  34.         for (int x = 0; x < ParaValu.Count; x++)
  35.         {
  36.  
  37.             cmd.Parameters.AddWithValue(ParaValu.GetKey(x).ToString(), ParaValu.GetByIndex(x).ToString());
  38.  
  39.         }
  40.         return RunUpdate();
  41.     }
  42.     private int RunUpdate(string UPDEIn) 
  43.     {
  44.         Initialize(CommandType.Text, UPDEIn);  
  45.         return RunUpdate();
  46.     }
  47.  
  48.     private DataTable RunQuery(string Select) 
  49.     {
  50.  
  51.         Initialize(CommandType.Text, Select);
  52.         Tbl = new DataTable();
  53.         Tbl.Load(cmd.ExecuteReader());
  54.         con.Close();
  55.         return Tbl;
  56.  
  57.     }
  58.  
  59.     private int RunUpdate()
  60.     {
  61.  
  62.         try
  63.         {
  64.             int x = cmd.ExecuteNonQuery();
  65.             con.Close();
  66.             return x;
  67.         }
  68.         catch (SqlException ex)
  69.         { 
  70.             con.Close();
  71.             return ex.Number;
  72.  
  73.         }
  74.  
  75.     }
  76. }
Aug 14 '14 #1
1 1184
Frinavale
9,735 Recognized Expert Moderator Expert
It would really help if you mentioned what line of code you are getting the Null Reference exception from.

But my guess is that you are getting it on line 23 of the above posted code..

Double check that your web.config contains a Connection String at position 1 (likely you want position 0)

-Frinny
Sep 8 '14 #2

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

Similar topics

1
6331
by: terminator800tlgm | last post by:
I don't understand the difference between ojbect handle and reference. For example, class Stack { private: CD cd_obj; // cd object DVD & dvd_ref;// dvd reference Why is it that dvd_ref can...
3
2651
by: vikenk | last post by:
Hello, This is my first post to this group. A little background first: I'm not not new to web-design, but new to manual coding :>) I'm trying to move from total dependence on FrontPage to more...
3
336
by: thejackofall | last post by:
Hi. I have a tricky question. Suppose I have a member or local variable named XYZ. I want to reference the object using a string, "XYZ". In code, I want to do the following. Checkbox...
6
2072
by: frankplank | last post by:
Hi *, I'm wondering if it is possible to create a C# object, and reference it *explicitly* in an excel document. I imagine this will be more possible if I programmatically populate the excel...
0
1542
by: Pedro Correia | last post by:
Hello, When i run my aspx i get this weird error: System.NullReferenceException: Object reference not set to an instance of an object. at emp.PAGTRF.InitializeComponent() at...
3
1560
by: Martin | last post by:
Hi Group I apologize for this very basic (I guess) question. I had a look in the posts and on MSDN but don't know where to start. I'm grateful for any push in the right direction. I just don't...
10
1889
by: Chris Morse | last post by:
Hi, I'm trying to figure out how to dynamically load an assembly and get an IClientPlugin interface to a class. I can get an "Object" reference to it after doing an "Activator.CreateInstance"...
2
1533
by: Rich | last post by:
Hello, I am trying to reference an object that already exists, ie, I have a dll project (dllProj) which contains a simple form. Then I have a main app and a secondary app which both have...
6
2208
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...
6
3387
by: kbin | last post by:
Hi all, I'm wondering if it is possible to have some sort of soft reference to an object's properties by using a string representing the property name. Let me explain by an example, here is my...
0
7218
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
7103
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
7307
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
7370
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...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.