Problems with Copy Records to Text File solution

Problem with the codes: copy/write the records from database to textfi

Page 1 of 1

4 Replies - 907 Views - Last Post: 02 September 2009 - 09:23 PM Rate Topic: -----

#1 dunk_6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 03-August 09

Problems with Copy Records to Text File solution

Posted 01 September 2009 - 11:29 PM

I have this code. Can help me why i cant run the codes?

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 


Is This A Good Question/Topic? 0
  • +

Replies To: Problems with Copy Records to Text File solution

#2 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Problems with Copy Records to Text File solution

Posted 01 September 2009 - 11:52 PM

Create an instance of streamwriter not like this
StreamWriter tw = File.AppendText("c:\\INMS.txt");			   


But like this ,I think so
 StreamWriter sw = new StreamWriter("c:\\INMS.txt");



Good luck

This post has been edited by debjit625: 02 September 2009 - 12:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 dunk_6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 03-August 09

Re: Problems with Copy Records to Text File solution

Posted 02 September 2009 - 07:50 PM

View Postdebjit625, on 1 Sep, 2009 - 10:52 PM, said:

Create an instance of streamwriter not like this
StreamWriter tw = File.AppendText("c:\\INMS.txt");			   


But like this ,I think so
 StreamWriter sw = new StreamWriter("c:\\INMS.txt");



Good luck



Dear debjit,

Thank for the reply, but I'm still having the same problem.!! The solution u give is about to create new object rite? but how about the file operation? i want to do append. Maybe it looks something like this?? But i'm not sure what to put inside the parameter..

StreamWriter tw = File.CreateText("c:\\INMS.txt");
				tw = File.CreateText("c:\\INMS.txt");


Thx
Was This Post Helpful? 0
  • +
  • -

#4 dunk_6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 03-August 09

Re: Problems with Copy Records to Text File solution

Posted 02 September 2009 - 08:07 PM

I change the code into this and it runs well but its using OleD.. Anyone knows how i can change it into sql server connection database? thank you..


//Insert into text file
  StreamWriter tw = File.AppendText("c:\\INMS.txt");
  OleDbDataReader reader = cmd.ExecuteReader();
  tw.WriteLine("id, ip_add, message, datetime");
  while (reader.Read())
  {
	 tw.Write(reader["id"].ToString());
	 tw.Write(", " + reader["ip_add"].ToString());
	 tw.Write(", " + reader["message"].ToString());
	 tw.WriteLine(", " + reader["datetime"].ToString());
   }
	 tw.WriteLine(DateTime.Now);
	 tw.WriteLine("---------------------------------");
	 tw.Close();			
	 reader.Close();


Was This Post Helpful? 0
  • +
  • -

#5 dunk_6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 03-August 09

Re: Problems with Copy Records to Text File solution

Posted 02 September 2009 - 09:23 PM

View Postdunk_6, on 2 Sep, 2009 - 07:07 PM, said:

I change the code into this and it runs well but its using OleD.. Anyone knows how i can change it into sql server connection database? thank you..


//Insert into text file
  StreamWriter tw = File.AppendText("c:\\INMS.txt");
  OleDbDataReader reader = cmd.ExecuteReader();
  tw.WriteLine("id, ip_add, message, datetime");
  while (reader.Read())
  {
	 tw.Write(reader["id"].ToString());
	 tw.Write(", " + reader["ip_add"].ToString());
	 tw.Write(", " + reader["message"].ToString());
	 tw.WriteLine(", " + reader["datetime"].ToString());
   }
	 tw.WriteLine(DateTime.Now);
	 tw.WriteLine("---------------------------------");
	 tw.Close();			
	 reader.Close();




YES, got already.. i change the OleDBDaraReader into SQLDataReader..

But if anyone had another approaches on this topics, please, i really appreciate it. thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1