i am new to ado.net and i have am having a problem in updating a database.
whenever i run the program an exception is raised.
i have checked the connection string and every thing is fine .
but when i try to update data into the database then this exception error arises.
i searched every where but unable to find any solution ...
following code...
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;
namespace WindowsFormsApplication16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Data.SqlClient.SqlConnection con;
DataSet ds1;
System.Data.SqlClient.SqlDataAdapter da;
int MaxRows = 0;
int inc = 0;
private void Form1_Load(object sender, EventArgs e)
{
con = new System.Data.SqlClient.SqlConnection();
//(@"Data Source=\SQLEXPRESS;AttachDbFilename=F:\MyWorkers1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.ConnectionString = "Data Source=\\SQLEXPRESS;AttachDbFilename=F:\\MyWorkers1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
ds1 = new DataSet();
string sql = "SELECT * from tblWorkers";
da =new System.Data.SqlClient.SqlDataAdapter(sql, con);
con.Open();
da.Fill(ds1, "Workers");
NavigateRecords();
MaxRows = ds1.Tables["Workers"].Rows.Count;
MessageBox.Show("open");
con.Close();
MessageBox.Show("close");
con.Dispose();
}
private void NavigateRecords()
{
DataRow dRow = ds1.Tables["Workers"].Rows[inc];//dataset me jo data rakha hay wo
//text boxes medaldo
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 != MaxRows - 1)
{
inc++;
NavigateRecords();
}
else
{ MessageBox.Show("last record"); }
}
private void button2_Click(object sender, EventArgs e)
{
if (inc != 0)
{
inc--;
NavigateRecords();
}
else { MessageBox.Show("first row"); }
}
private void button3_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{ inc = MaxRows - 1;
NavigateRecords();
}
}
private void button4_Click(object sender, EventArgs e)
{
if (inc != 0)
{ inc = 0;
NavigateRecords();
}
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void button6_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(da);
DataRow dRow = ds1.Tables["Workers"].NewRow();
dRow[1] = textBox1.Text;
dRow[2] = textBox2.Text;
dRow[3] = textBox3.Text;
ds1.Tables["Workers"].Rows.Add(dRow);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
da.Update(ds1, "Workers"); //exception is raised on this line...
MessageBox.Show("entery added into database");
}
}
}