Hi All,
For more than two days I m trying this problem. Some one
please help me out. My scenario is I want to directly insert data into
my table from the input which I get it from text boxes. Here is my
code...

con = new SqlConnection();
       con.ConnectionString = "Data Source=santhosh;Initial
Catalog=ecop;User ID=sa;Password=test";
       con.Open();
       SqlCommand cmd = new SqlCommand();
       cmd.CommandText="INSERT INTO
com(st_id,name,addres,phone,ct_type,brief_incident,suspect,date,time)
VALUES(@st_id,@name,@addres,@phone,@ct_type,@brief_incident,@suspect,@date,@time)";
       cmd.Parameters.Add(new
SqlParameter("@st_id",DropDownList1.SelectedValue));
       cmd.Parameters.Add(new SqlParameter("@name",TextBox1.Text));
       cmd.Parameters.Add(new SqlParameter("@addres",TextBox2.Text));
       cmd.Parameters.Add(new SqlParameter("@phone",TextBox3.Text));
       cmd.Parameters.Add(new
SqlParameter("@ct_type",DropDownList2.SelectedItem));
       cmd.Parameters.Add(new
SqlParameter("@brief_incident",TextBox6.Text));
       cmd.Parameters.Add(new
SqlParameter("@suspect",TextBox4.Text));
       cmd.Parameters.Add(new SqlParameter("@date",TextBox5.Text));
       cmd.Parameters.Add(new SqlParameter("@time",TextBox7.Text));
      [B] SqlDataReader  dr = cmd.ExecuteReader();[/B]------> In this
particular line I m getting Connection property has not been
initialized.

some one help me out to get ride of this problem...

With Regards,
Santhosh V Kumar

Recommended Answers

All 3 Replies

You haven't set the connection property of your command object.

cmd.Connection = con;

Yes you does not assign connection into your SqlCommand.

The connection string specifies information about how to initialize a connection to a data store.

It contains keyword/value pairs. the first pair is always the Provider or Driver. The following pairs will correspond with the data required to initialize the provide / driver.

Some Example Keywords:

SQL ODBC: Server, Database, Uid (user), Pwd (password), Trusted_Connection, MARS_Connection (multiple active record sets), Encrypt, AttachDbFilename,

SQLOLEDB: Data Source, Initial Catalog (database to connect to), UserId, Password, Integrated Security, Failover Partner (for mirroring).

Some example connection strings:

SqlConnection (.NET) standard Security:

"Data Source=ServerAddress;Initial Catalog=DataBase;User Id=username;Password=password;"

ODBC - PostgreSQL driver:

"Driver={PostgreSQL};Server=IP address;Port=5432;Database=DataBase;Uid=username;Pwd=password;"
=================
<<Snipped Site name>>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.