I am getting the following error:
"Executereader:commandtext property has not been initialised".
What is wrong in my code.Here i am exporting the data to a excel.
private void button1_Click(object sender, EventArgs e)
{
try
{
string fileName = String.Empty;
SaveFileDialog saveFileExcel = new SaveFileDialog();
saveFileExcel.Filter = "Text files |*.csv|All files (*.*)|*.*";
saveFileExcel.FilterIndex = 2;
saveFileExcel.RestoreDirectory = true;
if (saveFileExcel.ShowDialog() == DialogResult.OK)
{
fileName = saveFileExcel.FileName + ".csv";
CreateCSVFile(getDataTableForRetrieve(), fileName);
}
}
catch (Exception ex)
{
}
}
private DataTable getDataTableForRetrieve()
{
SqlConnection conn = null;
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
try
{
MyConnect myCnn = new MyConnect();
String connString = myCnn.getConnect().ToString();
SqlCommand command;
conn = new SqlConnection(connString);
command = new SqlCommand();
conn.Open();
string type = CmbTopCollection.Text;
string query = "";
if (type == "today")
{
query = "select userinfo.name, count(EMAIL.ID) as num,userdataloginfo.total_count from companyinfo COMPANY INNER JOIN emailinfo EMAIL ON COMPANY.dataID = EMAIL.DataID inner join userinfo ON COMPANY.userid = userinfo.ID inner join userdataloginfo on userdataloginfo.userid = userinfo.id where date >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) AND date < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1) group by userinfo.name,userdataloginfo.total_count order by num desc";
}
command = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
conn.Close();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message.ToString());
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
return dt;
}
public void CreateCSVFile(DataTable dt, string strFilePath)
{
StreamWriter sw = new StreamWriter(strFilePath, false, Encoding.GetEncoding("iso-8859-1"));
string csvStr = "";
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
csvStr = "\"" + dr[i].ToString().Replace(@"""", "") + "\"";
//csvStr.Replace(@"""", "");
sw.Write(csvStr);
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
MessageBox.Show("Exporting Completed");
}
Any help will be appreciated.