Home > Net >  The object reference not set to an instance of an object., link the database error, bosses help have
The object reference not set to an instance of an object., link the database error, bosses help have

Time:09-18


CodePudding user response:

You all know that connect to the database has a problem, then you can see the database connection string, right

CodePudding user response:

You have a problem is to know that links database, but can't find where there is a problem is a headache

CodePudding user response:

Give your connection string configuration hair

CodePudding user response:

Is your data is null, the breakpoint inside look, hiding inside the code to give you specific error

CodePudding user response:



Sqlhelper

using System;
using System.Data;
using System.Data.SqlClient;
Using System. The Configuration;
Using System. The Web;
Using System. Web. Security;
Using System. Web. UI.
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;

///
///SqlHelper summary description
///

Public class SqlHelper
{
Public static readonly string connstring=ConfigurationManager. ConnectionStrings [r]. "ConnectionString" ConnectionString;

Private SqlConnection con;
Private SqlCommand CMD.

Private SqlDataAdapter sda.
Private SqlDataReader SDRS.
Private DataSet ds;
Private DataView dv.

Public static SqlConnection getConnection ()//defined as static, it is very important!
{
Return new SqlConnection (connstring);
}
///
///open the database connection
///

Public void OpenDataBase ()
{
SqlConnection con=new SqlConnection (connstring);
con.Open();
}
///
///close the database connection
///

Public void CloseDataBase ()
{

con.Close();
Con. The Dispose ();

}
///
///returns the DataSet data set
///

///
/// Dataset
Public DataSet GetDs (string SqlStr, string TableName)
{
OpenDataBase ();
Sda=new SqlDataAdapter (SqlStr, con);
Ds=new DataSet ();
Sda. The Fill (ds, TableName);
CloseDataBase ();
Return the ds;
}
Public DataView GetDv (string SqlStr)
{
OpenDataBase ();
Sda=new SqlDataAdapter (SqlStr, con);
Ds=new DataSet ();
Sda. The Fill (ds);
Dv=ds. Tables [0]. DefaultView;
CloseDataBase ();
Return dv;

}
///
///returns the Datareader
///

///
/// The return value & lt;/returns>
Public SqlDataReader GetDataReader (string Sqlstr)
{
OpenDataBase ();
CMD=new SqlCommand (Sqlstr, con);
The SDR=CMD. ExecuteReader (System.Data.Com mandBehavior. CloseConnection);

Return the SDR.


}

///
///executing Sql statements method has no return value
///

///
Public void RunSql (string SqlStr)
{
OpenDataBase ();
CMD=new SqlCommand (SqlStr, con);
cmd.ExecuteNonQuery();
CloseDataBase ();
}
///
///returns the first line of the query results first
///

///
/// Returns the result & lt;/returns>
Public string ReturnSql (string SqlStr)
{
OpenDataBase ();
String ReturnSql="";
Try
{
CMD=new SqlCommand (SqlStr, con);
ReturnSql=CMD. ExecuteScalar (). The ToString ();
}
Catch {}
CloseDataBase ();
Return ReturnSql;
}
Public void DataCom (string SQLSTR)
{
OpenDataBase ();
SqlCommand sqlcom=new SqlCommand (SQLSTR, con);
Sqlcom. ExecuteNonQuery ();
CloseDataBase ();
}
Public void gvDataBind (the GridView gv, string SQLSTR)
{
OpenDataBase ();
SqlDataAdapter myda=new SqlDataAdapter (SQLSTR, con);
The DataSet myds=new DataSet ();
Myda. The Fill (myds);
Gv. The DataSource=myds;
Gv. DataBind ();
CloseDataBase ();
}
Public void dlDataBind (DataList dl, string SQLSTR)
{
OpenDataBase ();
SqlDataAdapter myda=new SqlDataAdapter (SQLSTR, con);
The DataSet myds=new DataSet ();
Myda. The Fill (myds);
Dl. The DataSource=myds;
Dl. DataBind ();
CloseDataBase ();
}
Public void ddlDataBind (DropDownList DDL, string SQLSTR, string DVF)
{
OpenDataBase ();
SqlDataAdapter myda=new SqlDataAdapter (SQLSTR, con);
The DataSet myds=new DataSet ();
Myda. The Fill (myds);
DDL. The DataSource=myds;
DDL. DataValueField=DVF;
DDL. DataBind ();
CloseDataBase ();
}

}
  • Related