problem ado.net application

M

mo/-/sin

hi. i m mohsin
i m making a gui application using c# at front end and sql server 2005
at backend...... i made a table in sql server 2005 with 4 columns and
in gui i used 4 text boxes and 4labels and three buttons. my full code
is mentioned below....... button1 is for add button2 is for save and
button3 is for delete...
the table is empty..but the lines prefix with ---- are giving
errors............ plz tel me why............ i also used a binding
navigator and bind all the columns correctly.............



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(1, 5);
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] = textBox3.Text;
dt.Rows.Add(dr);

sERVICESMAINTENANCETableAdapter.Update(cITY_HOSPITALDataSet);
textBox2.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)
{
string code;
code = textBox1.Text;
if (textBox1.Text.Length == 0)
{
MessageBox.Show("PLEASE SELECT A VALID RECORD");
}
else
{
dr =
cITY_HOSPITALDataSet.Tables["SERVICESMAINTENANCE"].Rows.Find(code);
dr.Delete();

sERVICESMAINTENANCETableAdapter.Update(cITY_HOSPITALDataSet);
}

}

}

}
 
M

Marc Gravell

but the lines prefix with ---- are giving errors

What errors? (please copy the exact error message)

The code you posted gives an idea, but we can't use that "as is" to
investigate anything. But if you give us the error message we might be
able to help.

Can I guess "NullReferenceException", "Object reference not set to an
instance of an object."?

In which case either the table isn't called "SERVICESMAINTENANCE"
inside the DataSet (just enumerate the .Tables to see what *is*
there), or you haven't loaded it correctly. Try using the table as:
dt = cITY_HOSPITALDataSet.SERVICE­SMAINTENANCE;

Or simply check that your DataSet is not completely empty.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top