Quote:"Object reference not set to an instance of an object." Null reference exception was unhandled.
private void button1_Click(object sender, EventArgs e) { OleDbConnection cnon = new OleDbConnection(); cnon.ConnectionString = @"Provider= Microsoft.ACE.OLEDB.12.0; Data Source=D:\WindowsFormsApplication1\barcode.accdb"; string query = "SELECT * FROM tblbarcode"; string sep = ","; string strFilePath = @"D:\WindowsFormsApplication1\export.txt"; StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath(strFilePath)); using (OleDbCommand Cmd = new OleDbCommand(query, cnon)) { cnon.Open(); using (OleDbDataReader dr = Cmd.ExecuteReader()) { int fields = dr.FieldCount - 1; while (dr.Read()) { StringBuilder sb = new StringBuilder(); for (int i = 0; i <= fields; i++) { if (i != fields) { sep = sep; } else { sep = ""; } sb.Append(dr[i].ToString() + sep); } sw.WriteLine(sb.ToString()); } } } }
using(StreamWriter sw = new StreamWriter(strFilePath)) using (OleDbCommand Cmd = new OleDbCommand(query, cnon)) { // read from db and write file }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)