问题描述
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"?