M
mo/-/sin
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 servicemaintenance 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 NullRefrenceException 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CITY_HOSPITAL
{
public partial class Form20 : Form
{
DataTable dt;
DataRow dr;
string code;
public Form20()
{
InitializeComponent();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void Form20_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the
'cITY_HOSPITALDataSet.SERVICESMAINTENANCE' table. You can move, or
remove it, as needed.
this.sERVICESMAINTENANCETableAdapter.Fill(this.cITY_HOSPITALDataSet.SERVICESMAINTENANCE);
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
button2.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
button2.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
int ctr, len;
string codeval;
dt = cITY_HOSPITALDataSet.Tables["SERVICESMAINTENANCE"];
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(object sender, EventArgs e)
{
dt = cITY_HOSPITALDataSet.Tables["SERVICESMAINTENANCE"];
dr = dt.NewRow();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text;
dr[2] = textBox3.Text;
dr[3] = textBox4.Text;
dt.Rows.Add(dr);
sERVICESMAINTENANCETableAdapter.Update(cITY_HOSPITALDataSet);
textBox1.Text = System.Convert.ToString(dr[0]);
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
this.sERVICESMAINTENANCETableAdapter.Fill(this.cITY_HOSPITALDataSet.SERVICESMAINTENANCE);
button1.Enabled = true;
button2.Enabled = false;
}
private void button3_Click(object sender, EventArgs e)
{
if (MessageBox.Show("ARE YOU SURE YOU WANT TO DELETE THIS
RECORD", "CONFIRMATION", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
string code;
code = textBox1.Text;
if (textBox1.Text.Length == 0)
{
MessageBox.Show("PLEASE SELECT A VALID RECORD",
"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
else
{
dr =
cITY_HOSPITALDataSet.Tables["SERVICESMAINTENANCE"].Rows.Find(code);
dr.Delete();
sERVICESMAINTENANCETableAdapter.Update(cITY_HOSPITALDataSet);
}
}
}
private void bindingNavigator1_RefreshItems(object sender,
EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void dataGridView1_Click(object sender, EventArgs e)
{
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
button2.Enabled = true;
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void button4_Click_1(object sender, EventArgs e)
{
dt = cITY_HOSPITALDataSet.Tables["SERVICESMAINTENANCE"];
dr = dt.Rows.Find(code);
------ dr.BeginEdit();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text.Trim();
dr[2] = textBox3.Text.Trim();
dr[3] = textBox4.Text.Trim();
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 servicemaintenance 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 NullRefrenceException 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CITY_HOSPITAL
{
public partial class Form20 : Form
{
DataTable dt;
DataRow dr;
string code;
public Form20()
{
InitializeComponent();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void Form20_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the
'cITY_HOSPITALDataSet.SERVICESMAINTENANCE' table. You can move, or
remove it, as needed.
this.sERVICESMAINTENANCETableAdapter.Fill(this.cITY_HOSPITALDataSet.SERVICESMAINTENANCE);
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
button2.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
button2.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
int ctr, len;
string codeval;
dt = cITY_HOSPITALDataSet.Tables["SERVICESMAINTENANCE"];
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(object sender, EventArgs e)
{
dt = cITY_HOSPITALDataSet.Tables["SERVICESMAINTENANCE"];
dr = dt.NewRow();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text;
dr[2] = textBox3.Text;
dr[3] = textBox4.Text;
dt.Rows.Add(dr);
sERVICESMAINTENANCETableAdapter.Update(cITY_HOSPITALDataSet);
textBox1.Text = System.Convert.ToString(dr[0]);
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
this.sERVICESMAINTENANCETableAdapter.Fill(this.cITY_HOSPITALDataSet.SERVICESMAINTENANCE);
button1.Enabled = true;
button2.Enabled = false;
}
private void button3_Click(object sender, EventArgs e)
{
if (MessageBox.Show("ARE YOU SURE YOU WANT TO DELETE THIS
RECORD", "CONFIRMATION", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
string code;
code = textBox1.Text;
if (textBox1.Text.Length == 0)
{
MessageBox.Show("PLEASE SELECT A VALID RECORD",
"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
else
{
dr =
cITY_HOSPITALDataSet.Tables["SERVICESMAINTENANCE"].Rows.Find(code);
dr.Delete();
sERVICESMAINTENANCETableAdapter.Update(cITY_HOSPITALDataSet);
}
}
}
private void bindingNavigator1_RefreshItems(object sender,
EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void dataGridView1_Click(object sender, EventArgs e)
{
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
button2.Enabled = true;
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void button4_Click_1(object sender, EventArgs e)
{
dt = cITY_HOSPITALDataSet.Tables["SERVICESMAINTENANCE"];
dr = dt.Rows.Find(code);
------ dr.BeginEdit();
dr[0] = textBox1.Text;
dr[1] = textBox2.Text.Trim();
dr[2] = textBox3.Text.Trim();
dr[3] = textBox4.Text.Trim();
dr.EndEdit();
}
}
}