I am exporting to text file.While exporting,i am getting this error "Object reference not set to an instance"
dt.Load(reader);//error
here i am getting the error as object reference not set to an instance of an object
private DataTable dataTblForRetrieveInTextFile()
{
DataTable dt = new DataTable();
SqlConnection conn = null;
dt = null;
try
{
Int64 currCat = Convert.ToInt64(cmbCategories.SelectedValue);
Int64 status = Convert.ToInt64(cmbAllStatus.SelectedValue);
Int64 country = Convert.ToInt64(cmbAllCountries.SelectedValue);
Int64 region = Convert.ToInt64(cmbRegions.SelectedValue);
string frmDate = dateTimePicker1.Value.ToString("MM-dd-yyyy");
string toDate = dateTimePicker2.Value.ToString("MM-dd-yyyy");
Int64 user = Convert.ToInt64(cmbUsers.SelectedValue);
string isDateSelected = Convert.ToString(dateTimePicker1.Checked);
string isRemovesSelected = checkBoxRemoves.Checked.ToString();
string isKeywordSelected = checkBoxKeywords.Checked.ToString();
string isDomainSelected = checkBoxDomain.Checked.ToString();
int isActive = 0;
string logfield = "";
if (chkApproved.Checked)
{
isActive = 1;
}
else
{
isActive = 0;
}
if (cmbCategories.SelectedIndex != -1)
{
logfield = cmbCategories.Text;
}
if (cmbAllStatus.SelectedIndex != -1)
{
logfield = logfield + "," + cmbAllStatus.Text;
}
if (cmbUsers.SelectedIndex != -1)
{
logfield = logfield + "," + cmbUsers.Text;
}
if (cmbAllCountries.SelectedIndex != -1)
{
logfield = logfield + "," + cmbAllCountries.Text;
}
if (cmbRegions.SelectedIndex != -1)
{
logfield = logfield + "," + cmbRegions.Text;
}
if (frmDate != toDate)
{
logfield = logfield + "," + frmDate;
}
if (toDate != "")
{
logfield = logfield + "," + toDate;
}
if (chkApproved.Checked == true)
{
logfield = logfield + "," + "Approved checked";
}
if (checkBoxKeywords.Checked == true)
{
logfield = logfield + "," + "Keywords checked";
}
if (checkBoxRemoves.Checked == true)
{
logfield = logfield + "," + "Removes checked";
}
if (checkBoxDomain.Checked == true)
{
logfield = logfield + "," + "Domain checked";
}
string todaydate = DateTime.Today.ToString("MM-dd-yyyy");
string time = DateTime.Now.ToString("h:mm:ss tt");
MyConnect myCnn = new MyConnect(); String connString = myCnn.getConnect().ToString();
SqlCommand command;
conn = new SqlConnection(connString);
command = new SqlCommand();
conn.Open();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "GetDataForTextFile1";
command.Parameters.AddWithValue("@currCat", currCat);
command.Parameters.AddWithValue("@fromdate", frmDate);
command.Parameters.AddWithValue("@todate", toDate);
command.Parameters.AddWithValue("@user", user);
command.Parameters.AddWithValue("@country", country);
command.Parameters.AddWithValue("@region", region);
command.Parameters.AddWithValue("@status", status);
command.Parameters.AddWithValue("@isDateSelected", isDateSelected);
command.Parameters.AddWithValue("@isRemovesSelected", isRemovesSelected);
command.Parameters.AddWithValue("@keyword", isKeywordSelected);
command.Parameters.AddWithValue("@domain", isDomainSelected);
command.Parameters.AddWithValue("@active", isActive);
SqlDataReader reader = command.ExecuteReader();
dt.Load(reader);//error
string count = dt.Rows.Count.ToString();
conn.Close();
//for log
SqlCommand command11;
command11 = new SqlCommand("insert into userlogexport(user_id,date,time,filter,count,Export_page,fileType) values('" + currentLoggedUserId + "','" + todaydate + "','" + time + "','" + logfield + "','" + count + "','retrieve_Data','Text')", conn);
conn.Open();
command11.ExecuteNonQuery();
conn.Close();
//command11.Connection = conn;
//command11.CommandType = CommandType.StoredProcedure;
//command11.CommandText = "savelogdetails";
//string ipAddress = Request.ServerVariables["REMOTE_ADDR"];
string message = "";
// message = "Ip Address: " + ipAddress;
message = message + "\n User Name: " + uName + "\n";
message = message + "\n Date: " + todaydate + " Time:" + time + "\n";
message = message + "\n filters:" + logfield + "\n";
message = message + "\n Rows count:" + count + "\n";
message = message + "\n FileType: text";
sendEmail("mahi@expogr.com", "Database Export report-Retrive Data", "db@expofairs.net", message);
}
catch (Exception e1)
{
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
return dt;
}
return value:0
result:emailids