SIGN UP MEMBER LOGIN:    
ARTICLE

DataBase Operations Using DataSet

Posted by Mayur Gujrathi Articles | ADO.NET in C# April 18, 2011
Let's see in this article how to use a dataset.
Reader Level:


Consider a table called employee having two columns employeeid and eployeename.

Here we will use the five buttons insert, update, delete, first, previous, next and new.

Using
  Microsoft.VisualBasic;
Class declarations
sqlConnection Con;sqldataadapter da;sqlcomandbuilder cb;dataset ds;int rno;

Under form load

Con=new  OleDbConnection("User Id=sa;pwd=123;database=dbname");
Da=new sqldataadapter("select * from employee",con);
Ds=new dataset();
Da.missingschemaschema=MissingSchemaAction.AddWithkey;
Da.Fill(ds,"employee");
Showdata();
Private  Void ShowData()
{
    textBox1.Text=ds.tables[].Rows[rno][0].ToString();
   
textBox2.Text=ds.tables[].Rows[rno][1].ToString();
}

Under First Button

Rno=0;
ShowData();

Under previous  Button

if(
rno>0);
{rno-=1; showdata();}
Else
messageBox.Show("First Record");

Under Next  Button

if(
rno<ds.Tables[0].Rows.Count-1)
    {rno+=1; showdata();}
Else
messageBox.Show("Last  Record");

Under Last  Button

Rno=ds.Tables[0].rows.count-1;
Showdata();

Under New Button

Textbox1.text=textbox2.text="";
Textbox1.readonly=false;textBox1.Focus();

How to add a New Record

To add a new record to the datatable adopt the following process:

  1. create a new empty row by calling method NewRow() on DataTable

  2. Assign values to new row by treating it as a single dimensional array

  3. call add method on data table and add row to RowsCollection.

Under InserButton

DataRow dr=ds.Tables[0].NewRow();
Dr[0]=textBox1.Text;
Dr[1]=textBox2.Text;
Ds.tables[0].Rows.Add(dr);
MessageBox.show("Record Added");
textBox1.Readonly=true;
button1.PerformClick();

Under Update Button

Ds.tables[0].Rows[rno][1]=textBox1.Text;

Under DeleteButton

Ds.tables[0].Rows[rno].Delete();
MessageBox.show("Record Deleted");


Under save Button

Cb=new SqlCommandBuilder(da);
Da.Update(ds,"students");
MessageBox.Show("Data Saved to database");

Under search Button

Int employeeid=int.parse(interaction.inputBox("Enter id to search","search",""100,100));
Datarow dr=ds.tables[0].Rows.Finf(employeeid);
If(dr!=null)
{
    Textbox1.text=dr[0].ToString();
   
TextBox2.Text=dr[1].ToString();
}
Else
MessageBox.Show("Invalid  id");}

Login to add your contents and source code to this article
share this article :
post comment
 

Dear sir I write this article in ms word thats why i could not take attention towards case sensitive and other things as you mentioned, but vs provides intelligence for inbuilt keywords,classes so i think there is not to worry about it, it is totally in c#

Posted by Mayur Gujrathi Apr 19, 2011

The names sqlConnection, sqldataadapter, sqlcomandbuilder and dataset are correct for VB but not correct for C#, since C# is case sensitive. There is a mis-match of case for con, da and ds; sometimes the first letter is capitalized and sometimes it is not. That works for VB but not C#. There is a mis-match of case for Showdata. There are other mis-matches of case, such as Else, Private and Void. The if statement in the previous Button has an extra semicolon that is not valid for either language. If you just put the code into a project and try to build it then you will find many errors.

Posted by Sam Hobbs Apr 19, 2011

@sam I did not found any vb.net code here? please show me where it is. It can be there, but i think it is totally in c#

Posted by Mayur Gujrathi Apr 19, 2011

Note that the sample code appears to be a mix of VB and C#.

Posted by Sam Hobbs Apr 18, 2011

Thanks for the understanding!!!

Posted by Karthikeyan Anbarasan Apr 18, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor