Dipa Ahuja

Dipa Ahuja

  • NA
  • 3.1k
  • 716.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)

0
bharat

bharat

  • 0
  • 12
  • 0
Jul 25 2009 6:01 AM
thank you... also is it possible to export entire form as a pdf or word document ??
0
Niradhip Chakraborty

Niradhip Chakraborty

  • 0
  • 4.6k
  • 820.5k
Jul 24 2009 7:36 PM


What is the problem you are facing?

You are hiding the first form and opening the second form. Once you close the second form it will not show the first form Untill you call it again.

Now if you want to close entire application which is running though you close the second form then add the following code in form2 buttonClick.

System.Windows.Forms.Application.Exit();