我有此错误,但任何人都无法找到该错误,无法找到该错误 [英] I have error in this, but am unable to find that error any one find out that error

查看:57
本文介绍了我有此错误,但任何人都无法找到该错误,无法找到该错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是

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:

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();
			}
		}
	}
}

推荐答案

您收到消息还是其他内容?
您可能需要添加一个问号,以使该变量可为空.
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;



但是,最好为变量分配新实例而不是null.您可以在尝试"之前执行此操作.但是要获得更好的代码,您应该使用using.



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.

using (SqlConnection sc = new SqlConnection(...) )
{
   ...
}


这样,总是自动清除sc .

祝您好运!


This way sc is always cleaned up automatically.

Good luck!


确保从配置中正确检索了连接字符串.

例如

Make sure your connection string is correctly being retrieved from the configuration.

e.g.

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



然后,您可以按照E.F. Nijboer的回答做类似...



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

using (SqlConnection sc = new SqlConnection(connectionString ))
{
   sc.Open();
   // Do some SQL work...
}


这篇关于我有此错误,但任何人都无法找到该错误,无法找到该错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆