gridview connect to sql server [select/update/edit/insert]

asp.net , csharp , sql server Cebu, Philippines
  • 9 years ago

    hi,

    i would like to know on how to store all information to the database.

    on my program i used "DataTable" to pass a certain value to the gridview controller but it doesn't store directly to the sql server database.

    sample to this as shown:

    if (!IsPostBack) { DataTable dt = new DataTable();

            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Gender");
    
            dt.Rows.Add("1", "Sheen Buhay", "Male");
            dt.Rows.Add("2", "Joji Buhay", "Female");
            dt.Rows.Add("3", "Oliver Buhay", "Male");
            dt.Rows.Add("4", "Elvie Buhay", "Female");
            dt.Rows.Add("5", "Steve Austin", "Male");
            dt.Rows.Add("6", "Alicia Keys", "Female");
    
            DTSample = dt;
            GridView1.DataSource = dt;
            GridView1.DataBind();
          }
    

    this program uses gridview to auto-populate the information to the textboxes for Id, Name & Gender without any database connection.

    what i would like to achieve here is i would like those data stored on the database and keep the same functionality for selecting, editing, updating directly from the gridview control.

    i have a sql command on insert, delete, update however it doesn't auto-populate to the grid if i select a certain record.

    i will provide the entire code for both aspx and aspx.cs extensions.

    default.aspx

    default.aspx.cs

    using System; using System.Data; using System.Web.UI; using System.Data.SqlClient; using System.Web.UI.WebControls;

    namespace WebApplication2 { public partial class _Default : System.Web.UI.Page {
    public DataTable DTSample { get { DataTable table = ViewState["DTSample"] as DataTable; return (table != null) ? table : null; } set { ViewState["DTSample"] = value; } }

       protected void Page_Load(object sender, EventArgs e)
        {
          if (!IsPostBack)
          {
            DataTable dt = new DataTable();
    
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Gender");
    
            dt.Rows.Add("1", "Sheen Buhay", "Male");
            dt.Rows.Add("2", "Joji Buhay", "Female");
            dt.Rows.Add("3", "Oliver Buhay",
    

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook