Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            datastructure();
        }

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }

    private void datastructure()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("FIRST NAME");
        dt.Columns.Add("CITY");
        ViewState["vst"] = dt;
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)ViewState["vst"];
        DataRow newrow = dt.NewRow();
        newrow["ID"] = TextBox1.Text;
        newrow["FIRST NAME"] = TextBox2.Text;
        newrow["CITY"] = TextBox3.Text;
        dt.Rows.Add(newrow);
        dt.AcceptChanges();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}




Now, I want to store this data which is displayed in gridview and stored in data table, into database table on one button click so pleas help me to solve this and tell me how to store this data into database?
Posted
Updated 20-Mar-12 8:34am
v4

1 solution

Which database are you using? If you are using SQL Server, easiest way is to go with LINQ. If it's another database, you must be more specific.

Take a look at those links, i've written some tutorials about it.

For SQL Server
http://codeabout.wordpress.com/2012/03/02/saving-files-into-your-sql-database-with-c/[^]

For Postgres using Npgsql
http://codeabout.wordpress.com/2012/03/08/working-with-postgres-database-and-c-by-using-npgsql-and-reflection/[^]

let me know if none of this was useful, regards and good luck
 
Share this answer
 
Comments
Member 8693833 20-Mar-12 14:03pm    
i went through your links but i didnt get proper solution can u pls post solution code for this ......?
Felipe Gentil 20-Mar-12 14:26pm    
You didnt answer my question. Which database are you using?
Member 8693833 20-Mar-12 14:39pm    
ohhh sorry i m using microsoft sql server 2008 r2
and plz give code for this i have already googled it out but i didnt get proper solution.......
Felipe Gentil 20-Mar-12 14:46pm    
If you are using sql server is easier, you must use linq to get it stored.
I dont have the code here, but i can help you out

Again, take a look at this tutorial and you will find out.
http://codeabout.wordpress.com/2012/03/02/saving-files-into-your-sql-database-with-c/

a few things you might need to consider
- create .dbml file (like i did in this tutorial)
- create the datacontext variable (which you will use to store into database)
- create an object that you will fill with your data coming from datagridview
- fill your object with data from datagridview
- get the table and insert the object
- submit changes

let me know if you still cant do it! i will find another way to help you.
good luck, regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900