用户代码未处理NullReferenceException 未将对象引用设置到对象的实例

问题描述

stringmystr=WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;用户代码未处理NullReferenceException未将对象引用设置到对象的实例求大神解决下面是全部代码usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Data.SqlClient;usingSystem.Data;usingSystem.Web.Configuration;///<summary>///commonclass的摘要说明///</summary>publicclassCommonClass{publicCommonClass(){////TODO:在此处添加构造函数逻辑//}publicSqlConnectionGetConnection(){stringmystr=WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;SqlConnectionmyconn=newSqlConnection(mystr);returnmyconn;}publicstringMessageBox(stringTxtMessage,stringUrl){stringstr;str="<scriptlanguage=javascript>alert('"+TxtMessage+"');location='"+Url+"'</script>";returnstr;}publicBooleanExecSQL(stringsqlStr){SqlConnectionmyconn=GetConnection();myconn.Open();SqlCommandmycmd=newSqlCommand(sqlStr,myconn);try{mycmd.ExecuteNonQuery();myconn.Close();}catch{myconn.Close();returnfalse;}returntrue;}publicDataSetGetDataSet(stringsqlstr,stringTableName){SqlConnectionmyconn=GetConnection();myconn.Open();SqlDataAdapteradapt=newSqlDataAdapter(sqlstr,myconn);DataSetds=newDataSet();adapt.Fill(ds,TableName);myconn.Close();returnds;}publicintcheckLogin(stringloginName,stringloginPwd){SqlConnectionmyconn=GetConnection();SqlCommandmycmd=newSqlCommand("selectcount(*)fromtb_userwhereName=@loginNameandPassWord=@loginPwd",myconn);mycmd.Parameters.Add(newSqlParameter("@loginName",SqlDbType.VarChar,20));mycmd.Parameters["@loginName"].Value=loginName;mycmd.Parameters.Add(newSqlParameter("@loginPwd",SqlDbType.VarChar,20));mycmd.Parameters["@loginPwd"].Value=loginPwd;myconn.Close();inti=Convert.ToInt32(mycmd.ExecuteScalar());mycmd.Dispose();mycmd.Clone();returni;}publicstringRandomNum(intn){stringstrchar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";string[]VcArray=strchar.Split(',');stringVNum="";inttemp=-1;Randomrand=newRandom();for(inti=1;i<n+1;i++){if(temp!=-1){rand=newRandom(i*temp*unchecked((int)DateTime.Now.Ticks));}intt=rand.Next(61);if(temp!=-1&&temp==t){returnRandomNum(n);}temp=t;VNum+=VcArray[t];}returnVNum;}publicSqlDataReaderExecReader(stringsqlstr){SqlConnectionmyconn=GetConnection();myconn.Open();SqlCommandcmd=newSqlCommand(sqlstr,myconn);SqlDataReadersdr=cmd.ExecuteReader();myconn.Close();returnsdr;}}

解决方案

解决方案二:
web.config文件里面的<connectionStrings>里面没有"ConnectionString"。stringmystr=WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;这句里面["ConnectionString"]部分要用web.config文件里面的<connectionStrings>里面定义的connectionstring的名字下面是例子stringmystr=WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

<connectionStrings><addname="NorthwindConnectionString"connectionString="DataSource=serverName;InitialCatalog=Northwind;PersistSecurityInfo=True;UserID=userName;Password=password"providerName="System.Data.SqlClient"/></connectionStrings>

解决方案三:
这要检查你的Web.Config,节点的name是不是"ConnectionString"?

时间: 2024-08-03 17:27:03

用户代码未处理NullReferenceException 未将对象引用设置到对象的实例的相关文章

用户代码未处理 System.NullReferenceException Message=未将对象引用设置到对象的实例。

问题描述 "/"应用程序中的服务器错误.未将对象引用设置到对象的实例.说明:执行当前Web请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.异常详细信息:System.NullReferenceException:未将对象引用设置到对象的实例.源错误:执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息.堆栈跟踪:[NullReferenceException:未将对象引用设置到对象的

net-用户代码NullReferenceException未将对象引用设置到对象的实例

问题描述 用户代码NullReferenceException未将对象引用设置到对象的实例 解决方案 temp没有查询出数据,返回了null 解决方案二: 未将对象引用设置到对象的实例.未处理NullReferenceExceptionSystem.NullReferenceException: 未将对象引用设置到对象的实例.System.NullReferenceException: 未将对象引用设置到对象的实例

错误解决:System.NullReferenceException: 未将对象引用设置到对象的实例

在ASP.NET开发中,遇到System.NullReferenceException: 未将对象引用设置到对象的实例的错误提示,解决方法如下:       (1)所设置的变量为空值或没有取到值,一般出现在传递参数的时候出现这个问题,也会在使用datagrid或gridview或datalist等数据控件时出现. (2)控件名称与codebehind里面的没有对应 (3)未用new初始化对象 (4)在程序中所引用的控件不存在 解决方法: (1)使用try..catch...finally捕捉错误

上传服务器后出现System.NullReferenceException: 未将对象引用设置到对象的实例。

问题描述 如题[NullReferenceException:未将对象引用设置到对象的实例.]TopWin.WebUI.game._3jymdd.Page_Load(Objectsender,EventArgse)ine:topwincmswebuigame3jymdd.aspx.cs:80System.Web.UI.Control.OnLoad(EventArgse)+67System.Web.UI.Control.LoadRecursive()+35System.Web.UI.Page.Pr

周末浅说--未将对象引用设置到对象的实例(System.NullReferenceException)

System.NullReferenceException:未将对象引用设置到对象的实例,这是一个新鸟,中鸟,老鸟都避不开的错误.   下面基础的解释一下这错误:   1:本质上的错误:   object a;//a是Null对象protected void Page_Load(object sender, EventArgs e) {     a.ToString();//调用一个Null对象的方法 }   当然啦!结果就如下图了:     这么赤裸裸的写出这种代码,不太容易,通常更倾向于下面

web窗体-System.NullReferenceException: 未将对象引用设置到对象的实例。

问题描述 System.NullReferenceException: 未将对象引用设置到对象的实例. 错误的问题! 未将对象引用设置到对象的实例. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例. 源错误: 行 40: 行 41: Application.Lock(); 行 42: ((Panel)Appl

“ResolveAssemblyReference”任务意外失败。System.NullReferenceException: 未将对象引用设置到对象的实例。

问题描述 "ResolveAssemblyReference"任务意外失败.System.NullReferenceException:未将对象引用设置到对象的实例. 解决方案 解决方案二:你这也太笼统了.贴出出问题的代码啊.这个只能判断出现了null值,你却引用他的属性或方法.所以报这个错.

.net-System.NullReferenceException: 未将对象引用设置到对象的实例

问题描述 System.NullReferenceException: 未将对象引用设置到对象的实例 private void CourceDetailsShow() { string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //取出连接字符串 string SqlStr = "SELECT Cource.*,Teacher.teaName,D

c#-C#:未将对象引用设置到对象的实例 (System.NullReferenceException)

问题描述 C#:未将对象引用设置到对象的实例 (System.NullReferenceException) 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace ConsoleExamples { static class Program { /// /// 应用程序的