submitting gridview data to database

Posted by Jammyhunt under ASP.NET on 3/12/2013 | Points: 10 | Views : 1369 | Status : [Member] | Replies : 10
hello,
i have a table like this

table1(gridview)

sl.no name salary department

1 raj 1000 maths

2 aman 1200 science

3 khan 900 computer

4 jammy 1100 others

now i want to create only one submit button, which will submit whole grid view data to my database




Responses

Posted by: Jayakumars on: 3/12/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

try this code

Your Submit Button Click Event.

protected void bt1_Click(object sender, EventArgs e)
{
bindgrid();
}

Your Grid Bind Method.
void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "Select * from dbo.Table_1";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "student");
GridView1.DataSource = ds;
GridView1.DataMember = "student";
GridView1.DataBind();
con.Close();
}

Mark as Answer if its helpful to you

Jammyhunt, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jammyhunt on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
what if i am using Ms-Access as my database

Jammyhunt, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

You can insert the values on the button click.
If you are using Ms-Access database.
You have to use oledbconnection.
For example:
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
string myInsertQuery = "INSERT INTO tablename(columnaname1,columnname2) Values('value1','value2')";
OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();


Thanks & Regards,
Santhi .V

Jammyhunt, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/12/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
try this code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class Default6 : System.Web.UI.Page
{
OleDbConnection con;

OleDbCommand com;

string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
protected void BtSubmit_Click(object sender, EventArgs e)
{
try
{
com = new OleDbCommand();
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\Test\\Test.mdb';Persist Security Info=False;");
con.Open();
com.Connection = con;
str = "Insert into Table1 values('" + TxtName.Text + "','" + TxtEmpno.Text + "','" + TxtAmount.Text + "')";
com.CommandText = str;
com.ExecuteNonQuery();
con.Close();
Response.Write("Records inserted successfully");
bindgrid();
}

catch (Exception ex)
{

ex.ToString();

}
}

void bindgrid()
{
DataTable dt;
OleDbDataAdapter Olesqlda;
com = new OleDbCommand();
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\Test\\Test.mdb';Persist Security Info=False;");
con.Open();
str = "Select * from Table1";
com = new OleDbCommand (str, con);
Olesqlda = new OleDbDataAdapter (com);
dt = new DataTable();
Olesqlda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
}


Mark as Answer if its helpful to you

Jammyhunt, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jammyhunt on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
(myConnectionString)
giving me errors

Jammyhunt, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
what error you are getting in connection string?

Jammyhunt, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jammyhunt on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
jayakumars i tried your code but it giving me this error

Exception Details: System.Data.OleDb.OleDbException: Unrecognized database format 'C:\Users\jammy\Documents\Visual Studio 2010\WebSites\testing\App_Data\TESTING.accdb'.

Jammyhunt, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/12/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

registered your OLEDB Provider and update your Accdb

Mark as Answer if its helpful to you

Jammyhunt, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jammyhunt on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
i have registered my OLEDB provider and i am using 2007 accdb

Jammyhunt, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 3/12/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Create employee table containing columns as slno,name,salary,department



protected void btn_insert_Click(object sender, EventArgs e)
{
foreach (GridViewRow g1 in GridView1.Rows)
{
SqlConnection con = new SqlConnection(connStr);
com = new SqlCommand("insert into employee(slno,name,salary,department) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();

}
Label1.Text = "Records inserted successfully";
}


If this post helps you mark it as answer
Thanks

Jammyhunt, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response