My program suppose to copy records from tblOutbox into tblSend. Once it was copied, the records from the tblOutbox is deleted. I want to add new function where, before the deletion, the records also copy/write into text files(i'm not sure what is the exact name to call). So the flow now is:
Open connection -> copy into tblSend and copy into textfile -> delete.
My problems is when i add the codes for textfile, it did not give any errors but still not run. When i try put try catch, this message appeared: Object reference not set to adn instance of an object. Why?
//create connection SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=F:\\Test2.mdf;Integrated Security=True;User Instance=True"); //command insert into queries SqlCommand cmd = new SqlCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = "INSERT INTO tblSend (ip, msg, date) SELECT ip, msg, date FROM tblOutbox"; cmd.Connection = sqlConnection1; //insert into text file StreamWriter tw = File.AppendText("c:\\INMS.txt"); SqlCommand cmdlog = new SqlCommand(); //(strLog, sqlConnection1); cmdlog .CommandType = System.Data .CommandType .Text ; cmdlog.CommandText = "SELECT * FROM tblOutbox"; cmdlog.Connection.Open(); SqlDataReader cursor = cmdlog.ExecuteReader(); while (cursor.Read()) tw.WriteLine(cursor["id"] + "\t" + cursor["ip"] + "\t" + cursor["msg"] + "\t" + cursor["date"]); cmdlog.Connection.Close(); tw.Close(); //command delete String strDel = "DELETE tblOutbox"; SqlCommand cmdDel = new SqlCommand(strDel, sqlConnection1); sqlConnection1.Open(); //open con cmd.ExecuteNonQuery(); //execute insert query cmdDel.ExecuteScalar();//execute delete query sqlConnection1.Close(); //close con