whenever i run the program an exception is raised.
error:uninitialized string
i have checked the connection string and every thing is fine .
i can navigate the records backward and forward
i can add data to the datased
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");
}
}
}
Posted Dec 13, 2012, 2:08 AM
your solution is not working for me i dont know why..
after a very long time of searching i found that my sql browser service was not running
i think that was the problem after that my problem is solved with following changes..
cb.DataAdapter.Update(ds1.Tables["Workers"]);
but one another thing i want to ask that
whenever i update a database ...i save it.. colse it ...and then reopen the form
but sometimes the changes are shown in the database but sometimes not why???
again thanks for your time...
Sandeep Singh ShekhawatPosted Dec 6, 2012, 11:59 PM
Change the schema. I made changes in bold look this in below code:
private void button6_Click(object sender, EventArgs e)
{
/*Fill DataSet with Schema*/
da.FillSchema(ds1, SchemaType.Source);
da.Fill(ds1);
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;
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(da);
da.Update(ds1, "Workers"); //exception is raised on this line...
MessageBox.Show("entery added into database");
}