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");
}
}
}

New Topic/Question
Reply



MultiQuote







|