Hi friends,
I am back with a new concept for ADO.NET. Here
we are create an application for generate a unique ID for every student. It is
very small example for all my viewers through this concept we can use and make same
more application.
In this application we are using some textboxes, labels,
datetimepicker, and form and button also. In this apps user can enter the name,
address and phone number with date, then “ID” will be generated automatically.
For all this phenomena we must make database for store the
IDs, names, addresses, contacts number and dates. We are using the sql2008 for
create data base.
Now let’s create database using SQL2008 :
CREATE DATABASE RAHUL
CREATE TABLE UNIQU
(
STUDENT_ID CHAR(15),
NAME VARCHAR(50),
ADDRESS VARCHAR(100),
CONTACT BIGINT,
DATE_OF_JOIN DATETIME,
DATE_OF_BIRTH DATETIME
)
INSERT INTO RAHUL VALUES (“0”,”0”,”0”,””,””)
Now execute the query.
This is the image after the query was executed successfully:
Now come for the design the windows form with using Visual
Studio 2010 and we can also choose another version of visual studio (but
V.S._2010 always supports SQL 2008).
I give a snapshot of visual studio design path:
Now we can go to the coding path of “form1” and write the code.
I am using c# language for this framework but you can also us the another .NET
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 WindowsApplication4
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
static int j; //global variable
int c;
SqlConnection con = newSqlConnection("Data
Source=.;Initial Catalog=rahul;Integrated Security=True");
//code for conection with sql
privatevoid button1_Click(object sender, EventArgs
e)
{
//Data Source=.;Integrated
Security=True
errorProvider1.Clear();
bool flag = true;
c = 0;
if (textBox2.Text.Length == 0)
{
errorProvider1.SetError(textBox2, "fill");
flag = false;
c = 1;
}
if (textBox3.Text.Length == 0)
{
errorProvider1.SetError(textBox3, "fill");
flag = false;
c = 1;
}
if (textBox4.Text.Length == 0)
{
errorProvider1.SetError(textBox4, "fill");
flag = false;
c = 1;
}
if (dateTimePicker2.Text.Length==0)
{
errorProvider1.SetError(dateTimePicker2, "update");
flag=false;
c = 1;
}
if (c == 0)
{
if (textBox4.Text.Length != 10)
{
MessageBox.Show("must
be 10 digit");
flag = false;
}
}
if (flag)
{
//try
//{
con.Open();
SqlCommand cmd = newSqlCommand("insert
into uniqu values('" + j + "','"
+ textBox2.Text + "','" +
textBox3.Text + "','" +
dateTimePicker1.Value + "','" +
dateTimePicker2.Value + "','" +
textBox4.Text + "')", con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("saved");
textBox2.Text = null;
textBox3.Text = null;
textBox4.Text = null;
j = j + 1;
textBox1.Text = "S1200" +
j.ToString();
}
else
{
MessageBox.Show("sorry");
}
//catch (Exception ex)
//{
//
MessageBox.Show("" + ex);
//}
con.Close();
}
/* else
{
MessageBox.Show("correct
error!");
}*/
}
privatevoid dateTimePicker2_ValueChanged(object sender, EventArgs
e)
{
}
privatevoid Form1_Load(object sender, EventArgs
e)
{
con.Open();
SqlCommand comm=newSqlCommand("select max(s_id) from ID ",con);
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
j = (int)dr[0] + 1;
textBox1.Text= "S1200" +
j.ToString();
}
con.Close();
dr.Close();
}
privatevoid textBox4_TextChanged(object sender, EventArgs
e)
{
}
}
}
Output come like this image:
If we click on the save button then error generated like this image
And if we fulfill all the text then student ID will be auto generated like this image:
Thank you!
If any problems have you face then kindly, Ask question
through comments I can try to help you.
0 comments:
Post a Comment