I am trying to write a routine that reads from 2 files and performs some analysis on the record, than writes the results to a seperate table. This should be very basic, but I am getting an error that there is already an open datareader associated with the command which must be closed.
here's my code
VB Code:
class Program { static void Main(string[] args) { SqlConnection conn = new SqlConnection("server=JXXX;database =database;user id=sa5; password=XXXX"); SqlCommand comm = new SqlCommand("select site.siteid,RawDataInput.loc from site,RawDataInput where RawDataInput.CALC_SITEID=site.siteid", conn); comm.Connection.Open(); SqlDataReader r = comm.ExecuteReader(CommandBehavior.CloseConnection); SqlCommand cmdnew = new SqlCommand("sudhir", conn); if (r.Read()) { Console.WriteLine(r["siteid"].ToString()); cmdnew.CommandText = "insert into sitecontroldata (siteid,location) values ('" + Function.ToInt(r["siteid"]) + "','" + r["loc"].ToString() + "')"; cmdnew.ExecuteNonQuery(); for (int i = 0; i <= 10; i++) { cmdnew.CommandText = "insert into sitecontroldata (siteid,location) values ('" + Function.ToInt(r["siteid"]) + "','" + r["loc"].ToString() + "')"; cmdnew.ExecuteNonQuery(); Console.WriteLine("Record inserted" + r["siteid"].ToString()); } } } }
any ideas?




Reply With Quote