Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The error is

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 24: { Line 25: 
Line 26: sc = new SqlConnection(ConfigurationManager.ConnectionStrings["CompMSConnectionString1"].ConnectionString); Line 27: sc = new SqlConnection(@"Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True"); 
Line 28: sc.Open();


This is my code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{

	protected void Page_Load(object sender, EventArgs e)
	{

	}
	protected void btnsub_Click(object sender, EventArgs e)
	{
		if (Page.IsPostBack)
		{
			SqlConnection sc = null;
			SqlCommand cmd = null;
			try
			{

				sc = new SqlConnection(ConfigurationManager.ConnectionStrings["CompMSConnectionString1"].ConnectionString);
				sc = new SqlConnection(@"Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");
				sc.Open();

				string query1 = String.Format(@"Insert Into student (rollno, fname, mname, lname) "+ " VALUES (@rollno, @fname, @mname, @lname)");
				cmd.Parameters.AddWithValue("@rollno",txtrollno.Text);
				cmd.Parameters.AddWithValue("@fname",txtfname.Text);
				cmd.Parameters.AddWithValue("@mname",txtmname.Text);
				cmd.Parameters.AddWithValue("@lname",txtlname.Text);
				cmd = new SqlCommand(query1, sc);
				cmd.ExecuteNonQuery();
				sc.Close();

				string query2 = String.Format(@"Insert Into studentmarks (rollno,marks) "+ " VALUES (@rollno, @marks)");
				cmd.Parameters.AddWithValue("@rollno",txtrol.Text);
				cmd.Parameters.AddWithValue("@marks", txtmarks.Text);
				cmd = new SqlCommand(query2, sc);
				cmd.ExecuteNonQuery();
			}
			finally // clean up
			{
				if(cmd != null)
					cmd.Dispose();
				if(sc != null)
					sc.Close();
			}
		}
	}
}
Posted
Updated 9-Mar-12 0:02am
v4
Comments
André Kraak 9-Mar-12 5:52am    
Edited question:
Added pre tags
Formatted text/code
Dylan Morley 9-Mar-12 5:53am    
What is the error?!
Dylan Morley 9-Mar-12 6:03am    
Updated question - moved multiple comments into question details
André Kraak 9-Mar-12 5:53am    
Please specify the error message you are getting.

If you wish to change your question use the Improve Question button.
OriginalGriff 9-Mar-12 5:53am    
And what is the error?
What is happening that shouldn't, or not happing that should?

Do you get a message or anything?
It might be you need to add a question mark to make the variable nullable.
SqlConnection? sc = null;


However, it would be better to assign the variable with the new instance instead of null. You can do that before the "try". But for even better code you should use using.
C#
using (SqlConnection sc = new SqlConnection(...) )
{
   ...
}

This way sc is always cleaned up automatically.

Good luck!
 
Share this answer
 
Make sure your connection string is correctly being retrieved from the configuration.

e.g.

C#
// Is this working? check your config - is 'CompMSConnectionString1' defined correctly?
string connectionString = ConfigurationManager.ConnectionStrings["CompMSConnectionString1"].ConnectionString);


then, you can follow E.F. Nijboer's answer to do something like...

C#
using (SqlConnection sc = new SqlConnection(connectionString ))
{
   sc.Open();
   // Do some SQL work...
}
 
Share this answer
 
Comments
rockpune 9-Mar-12 6:16am    
thanx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900