Inserting Values in Database

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()
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 = " ";
MessageBox.Show("Record not saved");


Author: Abhisek Panda19 Jul 2009 Member Level: Gold   Points : 0

someone kindly tell me why points are reduced?

Author: Deepika Haridas19 Jul 2009 Member Level: Gold   Points : 2


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,

Author: Abhisek Panda19 Jul 2009 Member Level: Gold   Points : 1

Thanks for your information. I will take care of it. Please tell me some tips to format well.

Abhisek Panda

Author: Deepika Haridas19 Jul 2009 Member Level: Gold   Points : 1


Check this link

Thanks & Regards,

Author: Abhisek Panda19 Jul 2009 Member Level: Gold   Points : 0

Thanks for your response.

Author: Er. Ram Singh20 Feb 2011 Member Level: Gold   Points : 1

SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
if (con.State == ConnectionState.Open)
Response.Write("Connection is Open");
Response.Write("Connection is Close");

GridViewRow drow = GridView1.SelectedRow;

//string i1 = drow.Cells[2].Text;
//string s = GridView1.Rows[i1].Cells[1].Text.ToString();
//GridView1.EditIndex = -1;

// i = 2;
//i = GridView1.SelectedRow.RowIndex;
string Email = ((TextBox)(GridView1.Rows[i].Cells[1].Controls[0])).Text.ToString();
int UserId = Convert.ToInt32(GridView1.Rows[i].Cells[0].Text);
string BPName = ((TextBox)(GridView1.Rows[i].Cells[2].Controls[0])).Text.ToString();
SqlCommand cmd = new SqlCommand("update Test1 set Email=@Email, BPName=@BPName where userid=@userid",con);
cmd.Parameters.Add("@BPName", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("@UserId", SqlDbType.Int);
cmd.Parameters[0].Value = Email;
cmd.Parameters[1].Value = BPName;
cmd.Parameters[2].Value = UserId;
GridView1.EditIndex = -1;
//DataSet ds = new DataSet();
//cmd.CommandType = CommandType.Text;
//SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText.ToString(), con);
//GridView1.DataSource = ds;

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: