Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecuteReader: Connection property has not been initialized

ExecuteReader: Connection property has not been initialized.

my coding is

protected void Button2_Click(object sender, EventArgs e)     {         SqlConnection conn = new SqlConnection("Data Source=Si-6\\SQLSERVER2005;Initial Catalog=rags;Integrated Security=SSPI");      SqlDataReader rdr = null;      try     {         // 2. Open the connection         conn.Open();          // 3. Pass the connection to a command object         //SqlCommand cmd = new SqlCommand("select * from Customers", conn);         SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)                   values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"')");          //         // 4. Use the connection         //          // get query results         rdr = cmd.ExecuteReader();          // print the CustomerID of each record         while (rdr.Read())         {             Console.WriteLine(rdr[0]);         }     }     finally     {         // close the reader         if (rdr != null)         {             rdr.Close();         }          // 5. Close the connection         if (conn != null)         {             conn.Close();         }     }   }   }      } 
like image 877
jeni Avatar asked May 03 '11 06:05

jeni


People also ask

What is ExecuteReader in SQL?

ExecuteReader() Sends the CommandText to the Connection and builds a SqlDataReader. ExecuteReader(CommandBehavior) Sends the CommandText to the Connection, and builds a SqlDataReader using one of the CommandBehavior values.

Does ExecuteReader close connection?

Ultimately it is the Close method of the data reader that will close the connection, provided nothing has gone wrong before. If there is an exception that occurs inside ExecuteReader or any of its called methods, before the actual DataReader object is constructed, then no, the connection will not be closed.

What is ExecuteReader and ADO Net?

ExecuteReader method is used to execute a SQL Command or storedprocedure returns a set of rows from the database. Example: public class Sample. { public string Test(int Id)


1 Answers

use this and pass connection object :

 SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"')",conn); 
like image 61
Govind Malviya Avatar answered Oct 11 '22 16:10

Govind Malviya