Unique Code Of Add Save and Delete Data in Ado.Net
First of all Create a table in SQL Server 2005, here I create a table name ‘student’ in database name ‘amitsaxena’ (you can give as you wish but take care the table name and database name because it required same as coding.)
Create a table with Id(int with Int Value as here create following column Student-Id, Name , village, Roll No, etc.)
=Now go to the visual studio 2005 and open a window application give the name and location as you wish. (take care of location of save file .)
Go to View menu and open Toolbox drag 7 label and seven text box and three buttons and a Binding Navigator (change the Lable text as you required)
here Student_Id, Name, Age, Roll No etc as you created in table column name.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; (//go to coding pageand add some system collection as you see here.)
namespace Filter
{
public partial class Form2: Form //go to the .cs I mean coding section and write code as mention here
{
DataTable dt;
DataRow drow;
string code;
public Form2()
{
InitializeComponent();
}
// now go to the form load section and write the following code
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'amitSaxenaDataSet1.Student' table. You can move, or remove it, as needed.
this.studentTableAdapter.Fill(this.amitSaxenaDataSet1.Student);
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
textBox5.Enabled = false;
textBox6.Enabled = false;
textBox7.Enabled = false;
button2.Enabled = false;
}
private void button1_Click(object sender, EventArgs e) // click on Button 1 and write code as below
{
button2.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
textBox6.Enabled = true;
textBox7.Enabled = true;
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
int ctr, len;
string codeval;
dt = amitSaxenaDataSet1.Tables["Student"];
len = dt.Rows.Count - 1;
drow = dt.Rows[len];
code = drow["Sid"].ToString();
codeval = code.Substring(1,9);
ctr = Convert.ToInt32(codeval);
if ((ctr >= 1) && (ctr < 9))
{
ctr = ctr + 1;
textBox1.Text = "S00" + ctr;
}
else if ((ctr >= 9) && (ctr < 99))
{
ctr = ctr + 1;
textBox1.Text = "S0" + ctr;
}
else if (ctr >= 99)
{
ctr = ctr + 1;
textBox1.Text = "S" + ctr;
}
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e) /// double click on button 2 and write the code as below)
{
dt = amitSaxenaDataSet1.Tables["Student"];
drow = dt.NewRow();
drow[0] = textBox1.Text;
drow[1] = textBox2.Text;
drow[2] = textBox3.Text;
drow[3] = textBox4.Text;
drow[4] = textBox5.Text;
drow[5] = textBox6.Text;
drow[6] = textBox7.Text;
dt.Rows.Add(drow);
studentTableAdapter.Update(amitSaxenaDataSet1);
textBox1.Text = System.Convert.ToString(drow[0]);
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
textBox5.Enabled = false;
textBox6.Enabled = false;
textBox7.Enabled = false;
this.studentTableAdapter.Fill(this.amitSaxenaDataSet1.Student);
button1.Enabled = true;
button2.Enabled = false;
}
private void button3_Click(object sender, EventArgs e) (double click on button three and write code)
{
string Code;
Code = textBox1.Text;
drow = amitSaxenaDataSet1.Tables["Student"].Rows.Find(Code);
drow.Delete();
studentTableAdapter.Update(amitSaxenaDataSet1);
}
private void button4_Click(object sender, EventArgs e) ( same as on 4th button)
{
this.Close();
}
}
}
Now Press the F5 button to run the program (for more information you mail me at amitsaxena0503@gamil.com)
have a nice day!!!!!!!