2 Replies - 4018 Views - Last Post: 27 February 2012 - 07:36 AM Rate Topic: -----

#1 shafikoyamma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-February 12

cant add or update sql database using c# as a biginer

Posted 26 February 2012 - 11:01 AM

hey friends, i am just beginer in c#. in visual studio . and studying using a online tutorial..
i want to know the resone why i cant update or save some datas to sql database table, i can pick the data, to dataset, add the data to data set, but i cant update database,
its showing "connection string property not initialized" error message
and highligt the
 ad.Update(dsa, "Workers" 
..
here my compleate code, which i got form my online tutorial,.. anybody pls help me,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace database_study
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        System.Data.SqlClient.SqlConnection con;
        DataSet dsa;
        System.Data.SqlClient.SqlDataAdapter ad;
        int rowMax = 0;
        int inc = 0;
        System.Data.SqlClient.SqlCommandBuilder cb; 
        private void Form1_Load(object sender, EventArgs e)
        {

            con = new System.Data.SqlClient.SqlConnection();
            dsa = new DataSet();
            cb = new System.Data.SqlClient.SqlCommandBuilder();
                        
            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\MyWorkers1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";            
            string sql = "SELECT*From tblWorkers";
            ad = new System.Data.SqlClient.SqlDataAdapter(sql, con);

            con.Open();
            ad.Fill(dsa,"Workers");
            rowMax = dsa.Tables["Workers"].Rows.Count;

            NavigateRecords();
            con.Close();
            con.Dispose();
            
            


        }
        private void NavigateRecords()
        {
            DataRow dRow = dsa.Tables["Workers"].Rows[inc];
            textBox1.Text = dRow.ItemArray.GetValue(1).ToString();
            textBox2.Text = dRow.ItemArray.GetValue(2).ToString();
            textBox3.Text = dRow.ItemArray.GetValue(3).ToString();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (inc != rowMax - 1)
            {
                inc++;
                NavigateRecords();

            }
            else
            { MessageBox.Show("No More Records"); }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (inc > 0)
            {
                inc--;
                NavigateRecords();
            }
            else { MessageBox.Show("this is the first Record"); }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(inc!=rowMax-1)
            { inc = rowMax - 1;
            NavigateRecords();
            }
            else{MessageBox.Show("this is the last record");}
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (inc != 0)
            {
                inc = 0;
                NavigateRecords();
            }
            else { MessageBox.Show("This is the first Record"); }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }
        
        private void btnSave_Click(object sender, EventArgs e)
        {
           System.Data.SqlClient.SqlCommandBuilder cb; 
            cb = new System.Data.SqlClient.SqlCommandBuilder(ad);
            
            DataRow dRow = dsa.Tables["Workers"].NewRow();
            dRow[1] = textBox1.Text;
            dRow[2] = textBox2.Text;
            dRow[3] = textBox3.Text;
            dsa.Tables["Workers"].Rows.Add(dRow);
            rowMax=rowMax +1;
            inc = rowMax - 1;
            con.Open();
            ad.Update(dsa, "Workers");
        
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlCommandBuilder cd;
            cd = new SqlCommandBuilder(ad);
            DataRow dRow2 = dsa.Tables["Workers"].Rows[inc];
            dRow2[1] = textBox1.Text;
            dRow2[2] = textBox2.Text;
            dRow2[3] = textBox3.Text;
            ad.Update(dsa, "Workers");

        }
        
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: cant add or update sql database using c# as a biginer

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9583
  • View blog
  • Posts: 36,319
  • Joined: 12-June 08

Re: cant add or update sql database using c# as a biginer

Posted 26 February 2012 - 12:41 PM

Try opening the connection BEFORE you set it to the adapter.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: cant add or update sql database using c# as a biginer

Posted 27 February 2012 - 07:36 AM

Take a look at this tutorial, which gives some good information about Add/Update/Delete with DataSet/DataAdapter.

http://www.dreaminco...iew-using-c%23/
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1