Dipa Ahuja

Dipa Ahuja

  • NA
  • 3.1k
  • 714.3k

query string for C#/sql

Jan 19 2010 12:15 PM
hi... i want to insert data in table 
this is my code:

 public partial class addcustomer : Form
    {
	//query string
        string q = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|db1.mdf;Integrated Security=True;User Instance=True";
        SqlConnection objconnection;

        public addcustomer()
        {
            InitializeComponent();
            objconnection = new SqlConnection(q);
            
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            objconnection.Open();

            SqlCommand comm = new SqlCommand("insert into customers values(@cid,@cname,@contact,@city)", objconnection);
            comm.Parameters.AddWithValue("@cid", 10);
            comm.Parameters.AddWithValue("@cname", txtname.Text.ToString());
            comm.Parameters.AddWithValue("@contact", txtcont.Text.ToString());
            comm.Parameters.AddWithValue("city", txtcity.Text.ToString());

            comm.ExecuteNonQuery();
            objconnection.Close();
            MessageBox.Show("Record Added");



        }
    }


at run time i added data no error found bt data are not inserted into table..

when i try to load data from dataset to datagrid i found connection is not establishd.

how to write querystring?



Answers (2)