How to insert data in DATABASE?
In this article I plan to demonstrate how to insert data from a SQL Server . This code should work on both SQL Server , I am using SQL 2005 & Visual Studio 2008, This code should work with both C# windows applications and C# web applications.
Background:
Part of my current project required me too store information from a database. I decided to use C# as my target language since I am currently reading Apress.Illustrated.C.Sharp.2008.Feb.2008, which by the way is a must have book.
Working:
1)First open Visual Studio 2008:

2)Creat C# NEW PROJECT

3)Select C# WINDOWS FORM APPLICATION

4)Creat simple form which have 2 textboxes (textbox1 & textbox2) ,2 lable(Name & Age)and 1 button (INSERT)

7)go to SERVER EXPLORER ,Database(MyDataBase.dbo) have been created then open the Database explorer then go to TABLE,right click TABLE and ADD NEW TABLE


9)and save Table NAme is MyTable

10)then go to DATA and Add New DataSource



11)choose your Data Source ,we use Microsoft SQL SERVER then Continue

*Server Name is ".\sqlexpress"
*Select your Database Name ,our database name is MyDataBase
*You can TEST CONNECTION ,it will be success,then OK

*and our project coneection string is MyDataBaseConnectionString

which Databse you want to choose?
check the Tables which is youu DataObject.. and then FINISH

in DataSource here is the MyTable DataGrid where we show the data
when we Drag the MyTable DataGrid to the form it will create 5 thing automatically
*myDataBaseDataSet
*myTableBindingSource
*myTableTableAdapter
*tableAdapterManager
*myTableBindingNavigator
you can delete the BINDING NAVIGATOR BAR.it is not necessary

Double click of INSERT button ,VS 2008 will creat automaticallyClick Event
then this is code which is use in Button Click event
MyDataBaseDataSet.MyTableRow mytable = myDataBaseDataSet.MyTable.NewMyTableRow();
//For NAME
mytable.Name = textBox1.Text;
//For Age
mytable.Age =Convert.ToInt32( textBox2.Text);
myDataBaseDataSet.MyTable.Rows.Add(mytable);
try
{
this.Validate();
this.myTableBindingSource.EndEdit();
this.myTableTableAdapter.Update(mytable);
}
catch(Exception ex)
{
MessageBox.Show(""+ex.Message);
}
after this coding Compile and Run the project..and insert the data...
this is data show in Grid



IF any queries about this project and other u can ask me on comments..
(next article will be publish with in 1 week)
Download project:http://www.4shared.com/file/108097511/86163c0/DatabaseConnectivity.html
BE a part of My Network:
Thanks
& Regards,
Rizwan Ahmed
Software Engineer And Microsoft Dynamics Technical Consultant
Cell Phone:+92 332 3588699
GTalk|LinkedIn|Twitter|Facebook|Blog|Skype
kudos for your effort.
ReplyDeleteNice work
ReplyDeletenice work dude...
ReplyDeletenext article publish rapidly plz..
Thanks. Good Work
ReplyDeletenIce wOrk
ReplyDeletei like it
ReplyDeletegr8 man
ReplyDeletenice work.............by Cute Sonu:)
ReplyDeletegood work
ReplyDeleteDhinkaa Chikaa Dhinkaa Chikaa Dhinka Chika Dhinka Chika ....
ReplyDeleteO O Oh!
This is a great introduction. However, the link doesn't seem to work anymore. Any chance you can repost the sample?
ReplyDeletevery nice thanks a lot
ReplyDeleteOK i will post again
ReplyDelete