Hi,
i am having problem in updating my database although i can upadate my dataset

private void btnSave_Click(object sender, EventArgs e)
        {
            DataRow dRow = ds1.Tables["ContainerID"].NewRow();

            dRow[1] = textBox1.Text;
            dRow[2] = textBox2.Text;
            dRow[3] = textBox3.Text;

            ds1.Tables["ContainerID"].Rows.Add(dRow);

            MaxRows = MaxRows + 1;
            inc = MaxRows - 1;

            MessageBox.Show("Entry Added");
        }

but while updating database it gives an error
The ConnectionString property has not been initialized.

private void btnSave_Click(object sender, EventArgs e)
        {            
            System.Data.SqlClient.SqlCommandBuilder cb;
            cb = new System.Data.SqlClient.SqlCommandBuilder(da);

            DataRow dRow = ds1.Tables["ContainerID"].NewRow();

            dRow[1] = textBox1.Text;
            dRow[2] = textBox2.Text;
            dRow[3] = textBox3.Text;

            ds1.Tables["ContainerID"].Rows.Add(dRow);

            MaxRows = MaxRows + 1;
            inc = MaxRows - 1;

            da.Update(ds1, "ContainerID");
            //ds1.AcceptChanges();
            MessageBox.Show("Entry Added");
        }

how can i Update my database?
Thanks.

Recommended Answers

show me where in code have you initialized the connection string

Jump to Post

in a button click you didn't open connection.

Jump to Post

All 5 Replies

show me where in code have you initialized the connection string

public Form1()
        {
            InitializeComponent();
        }
        SqlConnection con;
        DataSet ds1;
        SqlDataAdapter da;
        int MaxRows = 0;
        int inc = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new SqlConnection();
            ds1 = new DataSet();
            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\IMS.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            con.Open();

            string sql = "SELECT * From Container";
            da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
            da.Fill(ds1, "ContainerID");
            NavigateRecords();
            MaxRows = ds1.Tables["ContainerID"].Rows.Count;

            con.Close();


            con.Dispose();
        }

in a button click you didn't open connection.

yes now i did having error

Line 28 you dispose of the connection. You'll have to recreate it. And your latest post is no help at all. What error, what code?

Be a part of the DaniWeb community

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