Jump to content
Sign in to follow this  
naag

Field 'naag_database.Form1.da' is never assigned to, and will always have its default value null .

Recommended Posts

Hai... iam new for C# ...im learning ..i created a table and showing it...now i want to update,edit and delete the records...i did it in my way...Problem is whenever i give save ,addnew or delete button it shows "Object reference not set to an instance of an object."this error at line 120 " cb.DataAdapter.Update(ds1.Tables["workers"]);"my program. using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms; namespace naag_database{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } System.Data.SqlServerCe.SqlCeConnection con; System.Data.SqlServerCe.SqlCeDataAdapter da; DataSet ds1; int MaxRows = 0; int inc = 0; private void Form1_Load(object sender, EventArgs e) { con = new System.Data.SqlServerCe.SqlCeConnection(); con.ConnectionString = "DataSource=C:\\Users\\g.nagaraj\\Documents\\Databases\\naag.sdf"; con.Open(); System.Data.SqlServerCe.SqlCeDataAdapter da; string sql = "SELECT*from tbl_naag"; da = new System.Data.SqlServerCe.SqlCeDataAdapter(sql, con); ds1 = new DataSet(); MessageBox.Show("Connection Open"); da.Fill(ds1, "workers"); //GridItemCollection.Datasource = ds1; //GridItemCollection.Databind(); NavigateRecords(); MaxRows = ds1.Tables["Workers"].Rows.Count; con.Close(); } private void NavigateRecords() { DataRow dRow = ds1.Tables["Workers"].Rows[inc]; textBox1.Text = ds1.Tables["Workers"].Rows[inc].ItemArray.GetValue(4).ToString(); textBox2.Text = ds1.Tables["Workers"].Rows[inc].ItemArray.GetValue(3).ToString(); textBox3.Text = ds1.Tables["Workers"].Rows[inc].ItemArray.GetValue(2).ToString(); textBox4.Text = ds1.Tables["Workers"].Rows[inc].ItemArray.GetValue(1).ToString(); } private void btnNext_Click(object sender, EventArgs e) { if(inc !=MaxRows-1) { inc++; NavigateRecords(); } else { MessageBox.Show("No more Rows"); } } private void btnPrevious_Click(object sender, EventArgs e) { if (inc > 0) { inc--; NavigateRecords(); } else { MessageBox.Show("First Record"); } } private void btnLast_Click(object sender, EventArgs e) { if (inc != MaxRows - 1) { inc = MaxRows - 1; NavigateRecords(); } else { MessageBox.Show("Last record"); } } private void btnFirst_Click(object sender, EventArgs e) { if (inc != 0) { inc = 0; NavigateRecords(); } } private void btnNew_Click(object sender, EventArgs e) { textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); btnNew.Enabled =false; btnSave.Enabled =true; } private void updateDB() { System.Data.SqlServerCe.SqlCeCommandBuilder cb; cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(da); cb.DataAdapter.Update(ds1.Tables["workers"]); } private void btnSave_Click(object sender, EventArgs e) { DataRow dRow = ds1.Tables["Workers"].NewRow(); dRow[4] = textBox1.Text; dRow[3] = textBox2.Text; dRow[2] = textBox3.Text; dRow[1] = textBox4.Text; ds1.Tables["Workers"].Rows.Add(dRow); updateDB(); MaxRows = MaxRows + 1; inc = MaxRows - 1; btnNew.Enabled = true; btnSave.Enabled = false; } private void btnupdate_Click(object sender, EventArgs e) { DataRow dRow2 = ds1.Tables["workers"].Rows[inc]; dRow2[4] = textBox1.Text; dRow2[3] = textBox2.Text; dRow2[2] = textBox3.Text; dRow2[1] = textBox4.Text; updateDB(); MessageBox.Show("Data Updated"); } private void btndelete_Click(object sender, EventArgs e) { } private void label5_Click(object sender, EventArgs e) { } private void labelUpdate() { label4.Text = "Record " + (inc + 1) + " of " + MaxRows; } } }

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...