PDA

View Full Version : Object Refrence and...



faramarz_s
چهارشنبه 02 مهر 1382, 17:08 عصر
Error in '/Merat' Application.
--------------------------------------------------------------------------------

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 86: sad1.Fill(ds1,"tblSupplies");
Line 87: //-------------------------------------------------------------------------
Line 88: DataGrid1.DataSource=ds1.Tables["tblSupplies"].DefaultView;
Line 89



SqlConnection sc2=new SqlConnection("server=(local);uid=sa;pwd=yaali;database=MDB");
//-------------------------------------------------------------------------
String str_SQL =
" select * from tblSupplies";
//where name=N'" +TextBox1.Text.Trim() + "'";
//-------------------------------------------------------------------------
SqlCommand sqlcommandLogin = new SqlCommand(str_SQL, sc2);

sqlcommandLogin.Connection.Open();

SqlDataReader myReader = sqlcommandLogin.ExecuteReader();
//-------------------------------------------------------------------------
int i=0;
while(myReader.Read())
{
i++ ;
if (i>=1) break;
}
myReader.Close();

if (i>=1)
{
lblErrorMessage1.Text = "";
//Session["user_id"]=TextBox1.Text;
SqlDataAdapter sad1 = new
SqlDataAdapter("SELECT * FROM tblSupplies where name= N'"+
TextBox1.Text.Trim() + "'",sc2);
//-------------------------------------------------------------------------
DataSet ds1 = new DataSet();
sad1.Fill(ds1,"tblSupplies");
//-------------------------------------------------------------------------
DataGrid1.DataSource=ds1.Tables["tblSupplies"].DefaultView;
DataGrid1.DataBind();
//-------------------------------------------------------------------------
myReader.Close();
sc2.Close();
}
else
{
lblErrorMessage1.Text = "چنین رکوردی یافت نشد! ";
//-------------------------------------------------------------------------
SqlDataAdapter sad2 = new
SqlDataAdapter("SELECT * from tblSupplies where id=1 ",sc2);
//-------------------------------------------------------------------------
DataSet ds3 = new DataSet();
sad2.Fill(ds3,"tblSupplies");
//-------------------------------------------------------------------------
DataGrid1.DataSource=ds3.Tables["tblSupplies"].DefaultView;
DataGrid1.DataBind();
//-------------------------------------------------------------------------

myReader.Close();
// sqlconnectionPubs.Close();
sc2.Close();

Vahid_Nasiri
پنج شنبه 03 مهر 1382, 11:32 صبح
شما دوبار در کد

myReader.Close();
را صدا زده اید. بهتر است یکبار در آخر کار باشد.