7
Reply

object reference not set to an instance of an object

gouthami vasala

gouthami vasala

Oct 28 2009 7:28 AM
10.9k

i am currenlty building website in asp.net2.0
in which i export excel file into sql server
when iam running this code in localhost iam able to export excel data to sql server database but when iam runnign the same in my online server it is not getting export instead its giving an error
'object reference not set to an instance of an object'
i am not able to find it out the errror.
i also tried with dubugging but no such error is occuring but why its this error while in online.
the below is my code where export excel file into sql server
 
 
 
try
{
p =
this.lblFileName.Text;
this.ddlSheets.SelectedIndex = 1;
//Create connection string to Excel work book
string excelConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(p) + @";Extended Properties=""Excel 8.0;HDR=YES;""";
//Create Connection to Excel work book
OleDbConnection excelConnection =
new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand
(
"Select * from [" + this.ddlSheets.SelectedItem.Text + "]",
excelConnection);
excelConnection.Open();
dReader = cmd.ExecuteReader();
sqlBulk =
new SqlBulkCopy(strConnection);
sqlBulk.DestinationTableName =
"track_new";
bool flag = true;
while (dReader.Read())
{
string s = "";
string excel = "";
SqlConnection obj = new SqlConnection(strConnection);
// SqlCommand objcmd = new SqlCommand("select * from track_new", obj);
SqlDataAdapter da = new SqlDataAdapter("select * from track_new", obj);
DataSet ds = new DataSet();
obj.Open();
da.Fill(ds,
"track_new");
excel = dReader[0].ToString();
int i = 0;
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
//this.lblMessage.Text = objDataSet.Tables[0].TableName.ToString();
s = ds.Tables[0].Rows[i][0].ToString();
if (ds.Tables[0].Rows[i][0].ToString() == excel)
{
s = ds.Tables[0].Rows[i][0].ToString();
flag =
true;
break;
 
}
else
{
flag =
false;
continue;
}
}
if (flag == true)
{
this.lblMessage.Text = "\n" + "These Records Already Exists in the Database" + "\n";
// this.ListBox1.Visible = true;
this.ListBox1.Items.Add(s);
for (int j = 0; j < this.ListBox1.Items.Count; j++)
{
this.ListBox1.Items[j].Selected = true;
if (this.ListBox1.Items[j].Selected)
{
this.lblMessage.Text = this.lblMessage.Text + this.ListBox1.Items[j].Value.ToString() + ",\n";
}
}
 
 
}
//else
//{
// sqlBulk.WriteToServer(dReader);
// ClientScriptManager script = Page.ClientScript;
// script.RegisterStartupScript(this.GetType(), "alert", "alert('Records Successfully exported')", true);
// // dReader.Close();
//}
//obj.Close();
}
if (flag == false)
{
sqlBulk.WriteToServer(dReader);
ClientScriptManager script = Page.ClientScript;
script.RegisterStartupScript(
this.GetType(), "alert", "alert('Records Successfully exported')", true);
// dReader.Close();
}
}

catch (SqlException s)
{
this.lblMessage.Visible = true;
this.lblMessage.Text = s.Message.ToString();
}
 
please i really need help
 
 

Answers (7)