using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Excel;
using System.IO;
The error is
Object
reference not set to an instance of an object
The code is like this
on button click event
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlTransaction trans = null;
string s = "";
try
{
string UT = "UT1";
DataSet result = new DataSet();
//opening excel file which is uploaded by the user
System.IO.FileStream fs = File.Open(Server.MapPath("unittest.xlsx"), FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(fs);
excelReader.IsFirstRowAsColumnNames = true;
result = excelReader.AsDataSet();
// GridView gv = new GridView();
// gv.DataSource = result.Tables[0];
foreach (DataTable table in result.Tables)
{
foreach (DataRow row in table.Rows)
{
for (int i = 3; i < table.Columns.Count; i++)
{
mrk = row[i].ToString().Trim();
subject = row.Table.Columns[i].ColumnName;
if (table.TableName.Substring(0, 3) == "XII")
{
UT = "UT1";
}
else
UT = "UT1";
if (i == 3)
{
if (row[2].ToString().Trim() == "PCM-E")
{
subject = "ECO";
}
else if (row[2].ToString().Trim() == "PCB-IP")
{
subject = "IP";
}
else if (row[2].ToString().Trim() == "PCB-M")
{
subject = "MATHS";
}
else if (row[2].ToString().Trim() == "PCB-H")
{
subject = "HINDI";
}
else if (row[2].ToString().Trim() == "COM-M")
{
subject = "MATHS";
}
else if (row[2].ToString().Trim() == "COM-IP")
{
subject = "IP";
}
else if (row[2].ToString().Trim() == "COM-H")
{
subject = "HINDI";
}
else if (row[2].ToString().Trim() == "PCM-H")
{
subject = "HINDI";
}
else if (row[2].ToString().Trim() == "PCM-IP")
{
subject = "IP";
}
}
if (row[i].ToString() == "AB")
{
mrk = "NULL";
}
s = "insert into manojtempunittest(regno,subject,mark,class,ExType,acc_year) values('" + row[1] + "','" + subject + "'," + mrk + ",'" + table.TableName.Trim() + "','" + UT + "','2012')";
cmd.CommandText = s;
cmd.ExecuteNonQuery();
Manojmca
Member
11 Points
34 Posts
Referances Error regard........
Jan 19, 2013 06:34 AM|LINK
This is my name space Which i used.....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Excel;
using System.IO;
The error is
Object reference not set to an instance of an object
The code is like this
on button click event
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlTransaction trans = null;
string s = "";
try
{
string UT = "UT1";
DataSet result = new DataSet();
//opening excel file which is uploaded by the user
System.IO.FileStream fs = File.Open(Server.MapPath("unittest.xlsx"), FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(fs);
excelReader.IsFirstRowAsColumnNames = true;
result = excelReader.AsDataSet();
// GridView gv = new GridView();
// gv.DataSource = result.Tables[0];
//gv.DataBind();
//this.form1.Controls.Add(gv);
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["bcalConString"].ConnectionString;
con.Open();
trans = con.BeginTransaction();
cmd.Connection = con;
cmd.Transaction = trans;
string mrk = "";
string subject = "";
int count = 0;
foreach (DataTable table in result.Tables)
{
foreach (DataRow row in table.Rows)
{
for (int i = 3; i < table.Columns.Count; i++)
{
mrk = row[i].ToString().Trim();
subject = row.Table.Columns[i].ColumnName;
if (table.TableName.Substring(0, 3) == "XII")
{
UT = "UT1";
}
else
UT = "UT1";
if (i == 3)
{
if (row[2].ToString().Trim() == "PCM-E")
{
subject = "ECO";
}
else if (row[2].ToString().Trim() == "PCB-IP")
{
subject = "IP";
}
else if (row[2].ToString().Trim() == "PCB-M")
{
subject = "MATHS";
}
else if (row[2].ToString().Trim() == "PCB-H")
{
subject = "HINDI";
}
else if (row[2].ToString().Trim() == "COM-M")
{
subject = "MATHS";
}
else if (row[2].ToString().Trim() == "COM-IP")
{
subject = "IP";
}
else if (row[2].ToString().Trim() == "COM-H")
{
subject = "HINDI";
}
else if (row[2].ToString().Trim() == "PCM-H")
{
subject = "HINDI";
}
else if (row[2].ToString().Trim() == "PCM-IP")
{
subject = "IP";
}
}
if (row[i].ToString() == "AB")
{
mrk = "NULL";
}
s = "insert into manojtempunittest(regno,subject,mark,class,ExType,acc_year) values('" + row[1] + "','" + subject + "'," + mrk + ",'" + table.TableName.Trim() + "','" + UT + "','2012')";
cmd.CommandText = s;
cmd.ExecuteNonQuery();
}
}
}
trans.Commit();
}
catch (Exception rp)
{
Response.Write(rp.Message+"<br>"+s);
trans.Rollback();
}
finally
{
con.Close();
}
}
compile time there is no error but run time it give error as shown above..
Do u have any suggestion.............
oned_gk
All-Star
30969 Points
6338 Posts
Re: Referances Error regard........
Jan 19, 2013 07:37 AM|LINK
Pengzhen Son...
Star
8140 Points
837 Posts
Microsoft
Re: Referances Error regard........
Jan 21, 2013 10:34 AM|LINK
Hi,
I think you can set a breakpoint and debug it to see that which line code caused the exception.
Feedback to us
Develop and promote your apps in Windows Store
goel.ankit
Contributor
2531 Points
513 Posts
Re: Referances Error regard........
Jan 21, 2013 10:51 AM|LINK
Check if you are getting the File stream and excelReader objects, I suspect I of these are not coming through.
Ankit
(Please select 'Mark as Answer' if my response has helped you.)
Dheerendra
Member
97 Points
60 Posts
Re: Referances Error regard........
Jan 21, 2013 11:17 AM|LINK
You Can Add Referances------salution Expoler--Add Referance---.Net or Con --add --Microsoft.Tools.office.Word or Excel SoThat
goel.ankit
Contributor
2531 Points
513 Posts
Re: Referances Error regard........
Jan 25, 2013 08:45 AM|LINK
did you get chance to try the suggestions provided, has this issue been resolved?
Ankit
(Please select 'Mark as Answer' if my response has helped you.)