hi. i m mohsin.....
i m making a ado.net application using c# and gui............ there
are four text boxes and 4 buttons in the windows application.... ......
the application is connected to servicemaintena nce table which has 4
columns in sql server... thee 4 buttons in the application are button1
- add button2 - save button3 - delete and button4 - update... i hav
bind the application with the table.......... the coding for the whole
project is given below.......... .. first three buttons are working
properly however when i click on update button it throws an error and
the error is NullRefrenceExc eption was unhandled and below it is
written that
"Object reference not set to an instance of an object"........ .......
the line prefixed with '------' is giving error.......... ....
plz provide me with the right code........... ...
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Data.Sql Client;
namespace CITY_HOSPITAL
{
public partial class Form20 : Form
{
DataTable dt;
DataRow dr;
string code;
public Form20()
{
InitializeCompo nent();
}
private void textBox3_TextCh anged(object sender, EventArgs e)
{
}
private void Form20_Load(obj ect sender, EventArgs e)
{
// TODO: This line of code loads data into the
'cITY_HOSPITALD ataSet.SERVICES MAINTENANCE' table. You can move, or
remove it, as needed.
this.sERVICESMA INTENANCETableA dapter.Fill(thi s.cITY_HOSPITAL DataSet.SERVICE SMAINTENANCE);
textBox1.Enable d = false;
textBox2.Enable d = false;
textBox3.Enable d = false;
textBox4.Enable d = false;
button2.Enabled = false;
}
private void button1_Click(o bject sender, EventArgs e)
{
button2.Enabled = true;
textBox2.Enable d = true;
textBox3.Enable d = true;
textBox4.Enable d = true;
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
int ctr, len;
string codeval;
dt = cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"];
len = dt.Rows.Count -1;
dr = dt.Rows[len];
code = dr["SERVICEID"].ToString();
codeval = code.Substring( 5, 1);
ctr = Convert.ToInt32 (codeval);
if ((ctr >= 1) && (ctr < 9))
{
ctr = ctr + 1;
textBox1.Text = "SER00" + ctr;
}
else if ((ctr >= 9) && (ctr < 99))
{
ctr = ctr + 1;
textBox1.Text = "SER0" + ctr;
}
else if (ctr >= 99)
{
ctr = ctr + 1;
textBox1.Text = "SER" + ctr;
}
button1.Enabled = false;
}
private void button2_Click(o bject sender, EventArgs e)
{
dt = cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"];
dr = dt.NewRow();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text;
dr[2] = textBox3.Text;
dr[3] = textBox4.Text;
dt.Rows.Add(dr) ;
sERVICESMAINTEN ANCETableAdapte r.Update(cITY_H OSPITALDataSet) ;
textBox1.Text = System.Convert. ToString(dr[0]);
textBox1.Enable d = false;
textBox2.Enable d = false;
textBox3.Enable d = false;
textBox4.Enable d = false;
this.sERVICESMA INTENANCETableA dapter.Fill(thi s.cITY_HOSPITAL DataSet.SERVICE SMAINTENANCE);
button1.Enabled = true;
button2.Enabled = false;
}
private void button3_Click(o bject sender, EventArgs e)
{
if (MessageBox.Sho w("ARE YOU SURE YOU WANT TO DELETE THIS
RECORD", "CONFIRMATI ON", MessageBoxButto ns.YesNo,
MessageBoxIcon. Question) == DialogResult.Ye s)
{
string code;
code = textBox1.Text;
if (textBox1.Text. Length == 0)
{
MessageBox.Show ("PLEASE SELECT A VALID RECORD",
"ERROR", MessageBoxButto ns.OK, MessageBoxIcon. Error );
}
else
{
dr =
cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"].Rows.Find(code );
dr.Delete();
sERVICESMAINTEN ANCETableAdapte r.Update(cITY_H OSPITALDataSet) ;
}
}
}
private void bindingNavigato r1_RefreshItems (object sender,
EventArgs e)
{
}
private void groupBox1_Enter (object sender, EventArgs e)
{
}
private void button4_Click(o bject sender, EventArgs e)
{
}
private void dataGridView1_C lick(object sender, EventArgs e)
{
textBox1.Enable d = true;
textBox2.Enable d = true;
textBox3.Enable d = true;
textBox4.Enable d = true;
button2.Enabled = true;
}
private void groupBox2_Enter (object sender, EventArgs e)
{
}
private void button4_Click_1 (object sender, EventArgs e)
{
dt = cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"];
dr = dt.Rows.Find(co de);
------ dr.BeginEdit();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text.T rim();
dr[2] = textBox3.Text.T rim();
dr[3] = textBox4.Text.T rim();
dr.EndEdit();
}
}
}
i m making a ado.net application using c# and gui............ there
are four text boxes and 4 buttons in the windows application.... ......
the application is connected to servicemaintena nce table which has 4
columns in sql server... thee 4 buttons in the application are button1
- add button2 - save button3 - delete and button4 - update... i hav
bind the application with the table.......... the coding for the whole
project is given below.......... .. first three buttons are working
properly however when i click on update button it throws an error and
the error is NullRefrenceExc eption was unhandled and below it is
written that
"Object reference not set to an instance of an object"........ .......
the line prefixed with '------' is giving error.......... ....
plz provide me with the right code........... ...
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Data.Sql Client;
namespace CITY_HOSPITAL
{
public partial class Form20 : Form
{
DataTable dt;
DataRow dr;
string code;
public Form20()
{
InitializeCompo nent();
}
private void textBox3_TextCh anged(object sender, EventArgs e)
{
}
private void Form20_Load(obj ect sender, EventArgs e)
{
// TODO: This line of code loads data into the
'cITY_HOSPITALD ataSet.SERVICES MAINTENANCE' table. You can move, or
remove it, as needed.
this.sERVICESMA INTENANCETableA dapter.Fill(thi s.cITY_HOSPITAL DataSet.SERVICE SMAINTENANCE);
textBox1.Enable d = false;
textBox2.Enable d = false;
textBox3.Enable d = false;
textBox4.Enable d = false;
button2.Enabled = false;
}
private void button1_Click(o bject sender, EventArgs e)
{
button2.Enabled = true;
textBox2.Enable d = true;
textBox3.Enable d = true;
textBox4.Enable d = true;
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
int ctr, len;
string codeval;
dt = cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"];
len = dt.Rows.Count -1;
dr = dt.Rows[len];
code = dr["SERVICEID"].ToString();
codeval = code.Substring( 5, 1);
ctr = Convert.ToInt32 (codeval);
if ((ctr >= 1) && (ctr < 9))
{
ctr = ctr + 1;
textBox1.Text = "SER00" + ctr;
}
else if ((ctr >= 9) && (ctr < 99))
{
ctr = ctr + 1;
textBox1.Text = "SER0" + ctr;
}
else if (ctr >= 99)
{
ctr = ctr + 1;
textBox1.Text = "SER" + ctr;
}
button1.Enabled = false;
}
private void button2_Click(o bject sender, EventArgs e)
{
dt = cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"];
dr = dt.NewRow();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text;
dr[2] = textBox3.Text;
dr[3] = textBox4.Text;
dt.Rows.Add(dr) ;
sERVICESMAINTEN ANCETableAdapte r.Update(cITY_H OSPITALDataSet) ;
textBox1.Text = System.Convert. ToString(dr[0]);
textBox1.Enable d = false;
textBox2.Enable d = false;
textBox3.Enable d = false;
textBox4.Enable d = false;
this.sERVICESMA INTENANCETableA dapter.Fill(thi s.cITY_HOSPITAL DataSet.SERVICE SMAINTENANCE);
button1.Enabled = true;
button2.Enabled = false;
}
private void button3_Click(o bject sender, EventArgs e)
{
if (MessageBox.Sho w("ARE YOU SURE YOU WANT TO DELETE THIS
RECORD", "CONFIRMATI ON", MessageBoxButto ns.YesNo,
MessageBoxIcon. Question) == DialogResult.Ye s)
{
string code;
code = textBox1.Text;
if (textBox1.Text. Length == 0)
{
MessageBox.Show ("PLEASE SELECT A VALID RECORD",
"ERROR", MessageBoxButto ns.OK, MessageBoxIcon. Error );
}
else
{
dr =
cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"].Rows.Find(code );
dr.Delete();
sERVICESMAINTEN ANCETableAdapte r.Update(cITY_H OSPITALDataSet) ;
}
}
}
private void bindingNavigato r1_RefreshItems (object sender,
EventArgs e)
{
}
private void groupBox1_Enter (object sender, EventArgs e)
{
}
private void button4_Click(o bject sender, EventArgs e)
{
}
private void dataGridView1_C lick(object sender, EventArgs e)
{
textBox1.Enable d = true;
textBox2.Enable d = true;
textBox3.Enable d = true;
textBox4.Enable d = true;
button2.Enabled = true;
}
private void groupBox2_Enter (object sender, EventArgs e)
{
}
private void button4_Click_1 (object sender, EventArgs e)
{
dt = cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"];
dr = dt.Rows.Find(co de);
------ dr.BeginEdit();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text.T rim();
dr[2] = textBox3.Text.T rim();
dr[3] = textBox4.Text.T rim();
dr.EndEdit();
}
}
}
Comment