C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Databinding »

Inserting Values in Database


Posted Date: 18 Jul 2009    Resource Type: Code Snippets    Category: Databinding
Author: Abhisek PandaMember Level: Gold    
Rating: 1 out of 5Points: 8



Description :


First you have to create a simple database in access with only one table(Student) containing fields sname and sroll. Then save it and close it.

Now design a form to provide the user an interface to store data. The form contains two labels(Name, Roll) and two text boxes representing name and roll. Here the name of the text boxes are txtName and txtRoll.

Also add a button "Save" to the form and change its name to btnSave.Clicking on this button will save the value we are passing inside the textboxes into the database table.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb

namespace InsertData
{
public partial class Form1 : Form
{
//creating instances of Command and Connection class
OleDbCommand cmd;
OleDbConnection con;
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(Object Sender,EventArgs e)
{
int c;
this.con = new OleDbConnection();
//passing the database path and provider name
this.con.ConnectionString = "Data Source = d:\\Student.mdb;Provider = Microsoft.Jet.Oledb4.0";
this.cmd = new OleDbCommand();
this.cmd.Connection = this.con;
this.cmd.CommandType = CommandType.Text;
//passing insert command to insert values into the database table
this.cmd.CommandText = "insert into Student values('" + this.txtName.Text"' + '"this.txtRoll.Text + "')";
this.con = open();
//checking whether command successfully executed
c = this.cmd.ExecuteNonQuery();
if(c > 0)
{
MessageBox.Show("Record inserted");
this.txtName.Text = " ";
this.txtRoll.Text = " ";
this.txtName.Focus();
}
else
MessageBox.Show("Record not saved");
}
}
}





Responses

Author: Abhisek Panda    19 Jul 2009Member Level: Gold   Points : 0
someone kindly tell me why points are reduced?


Author: Deepika Haridas    19 Jul 2009Member Level: Diamond   Points : 2
Hi,

As it was not formatted your points are reduced. Currently I have formatted it . Better take care of it next time. Format it very well to gain more points.

--
Thanks & Regards,
Deepika
Editor


Author: Abhisek Panda    19 Jul 2009Member Level: Gold   Points : 1
Thanks for your information. I will take care of it. Please tell me some tips to format well.

Thanks,
Abhisek Panda


Author: Deepika Haridas    19 Jul 2009Member Level: Diamond   Points : 1
Hi,

Check this link

http://www.dotnetspider.com/resources/30247-Tips-for-Decorating-your-resources-get-Max.aspx

--
Thanks & Regards,
Deepika
Editor


Author: Abhisek Panda    19 Jul 2009Member Level: Gold   Points : 0
Thanks for your response.




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Inserting value to database  .  Inserting value to database  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Populate DataGridView using CSV File
Previous Resource: Acessing data from SQL server using Dataset
Return to Discussion Resource Index
Post New Resource
Category: Databinding


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use