Here I'm creating an application which can Insert,Edit and Delete the data in a SQL database.For this I used Visual studio 2008 and SQL server 2005.
The application consist of two forms,a Main form and a Sub form.When the user clicks the Insert or Edit button in the Main form,which will show the sub form,through that we can insert or edit the data.The inserted or edited data will show on the datagridView in the Main form.
In the database part we have to create a table named "Employee" as shown in fig.
Add some datas to the table as follows
Next step is to create UI for the application.For this open visual studio2008 and create a new project with the name DB_blog.Then rename the Form1 in the solution explorer by
Main.Set the following properties to the form.
MaximizeBox = False
startPosition = centerScreen
Text = Main
FormBorderStyle = Fixed3D
Add three buttons control and a datagridview control to the Main form from toolbox.Set the following properties to the controls.
button1
Name = btn_Insert
Text = Insert
button2
Name = btn_Edit
Text = Edit
button3
Name = btn_Delete
Text = Delete
dataGridView1
allowUserToAddrows = False
allowUserToResizeColumns = False
allowUserToResizeRows = False
MultiSelect = False
ReadOnly = True
RowHeadersVisible = False
selectionMode = FullRowSelect
Next step is to bind data source with the datagridview.For this select the datagridview and click the balck arrow on top.Then select choose Data Source and click Add Project Data Source and select the Database and click next.Then do the following steps .
Then select the table you want to show in datagridview and click finish.
Now our UI will like this.
Now we are going to create Sub form.For this add a new form to the project by clicking project in the menu and click add new item.Then select Windows forms in catagories and Windows form in templates.Then given the name SubForm and click add.Now a a new form will create.Set its Text property to blank.Add two textbox control,two label control and two button control.Set following properties to the controls
label1
Text = Name
label2
Text = Role
textbox1
Name = txt_Name
Modifier = public
textbox2
Name = txt_Role
Modifier = public
button1
Name = btn_OK
Text = OK
DialogResult = OK
button2
Name = btn_Cancel
Text =CancelDialogResult = Cancel
Our subForm will looks like this
INSERT
My idea is to show the sub form,when the user click the Insert button.After entering data in the textboxes,press OK button to save the data in the database.
For this to happen we have to create some codes in the click event of the Insert button in the Main form.To go to click event of the Insert button,double click the Insert button in the design view.Function InsertData is used to saving the data to the database.We need a connection string to connect with the database.To get the connection string easily,open server explorer in visual studio,right click the Data Connections and click Add Connection.Then enter server name and select database and click Ok.Then right click the database and click Properties.Copy the connection string and stored in a string variable.We need this connection string through out the programs,so we declared it as global.Add the following namespace
System.Data.SqlClient
Write the following code in Insert button click event.
InsertData function is showing below.
Our Insert section is finished.Press F5 to run the code.After clicking Insert button application looks like this.
EDIT
To edit an item,click the Edit button after selecting the row you want to edit.Then the sub form will open with the selected data s.There we edit and click Ok to save.The selected data in the main form is given to the sub form by its constructor.The constructor is shown below
Add the following code into the click event of the Edit button
UpdateData function is shown below.
Our Edit section is finished.Run the project and click Edit button,it will looks like this.
DELETE
To delete an entry,select the row you want to delete and click Delete button.
Add the following code in the click event of Delete button.
DeleteData function is shown below.
Our project is completed.