Home > ASP.NET > How to bind gridview from database.



How to bind gridview from database.

In this article we have described how to bind gridview from database in Sql server.


In this article we have described how to bind gridview from database in Sql server with a sample example and sample code. here you can have a look at it and learn

   

About Gridview

Gridview is a control in asp.net.

Gridview is a very popular and useful control in C#. We can specify custom columns and styles.

Gridview automatically binds to and displays data from a data source control.

 

Required Namespaces:

you will need to use the following namespaces.

using Newtonsoft.Json;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

 

aspx page

We have used the gridview code below. You can have a look at it and learn.

<html>

<head runat="server">

    <title></title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

</head>

<body>

    <form id="form1" runat="server">

        <div class="form-row">

            <div class="form-group col-md-2">

                <label for="inputName">Name</label>

                <asp:TextBox CssClass="form-control" ID="txtName" runat="server" />

            </div>

            <div class="form-group col-md-2">

                <label for="InputRole">Role</label>

                <asp:DropDownList CssClass="form-control" ID="ddlRole" runat="server">

                    <asp:ListItem Value="0" Text="Select" />

                    <asp:ListItem Value="1" Text="Admin" />

                    <asp:ListItem Value="2" Text="Vice-Admin" />

                    <asp:ListItem Value="3" Text="Member" />

                </asp:DropDownList>

            </div>

        </div>

        <br />

        <asp:Button Text="Submit" CssClass="btn btn-success" runat="server" OnClick="OnSubmit" />

        <br />

        <asp:GridView ID="gvUsers" CssClass="table table-bordered" runat="server">

        </asp:GridView>

    </form>

</body>

</html>

 

C# code

We have explained below C# code and added the examples of some user information in this article .You can have a look at it and learn.

protected void Page_Load(object sender, EventArgs e)

{

    if (!IsPostBack)

    {

 

        this.GetData();

    }

}

private void GetData()

{

    string config = Convert.ToString(ConfigurationManager.ConnectionStrings["myconnection"]);

    SqlConnection con = new SqlConnection(config);

    SqlCommand cmd = new SqlCommand("select * from  users", con);

    SqlDataAdapter sda = new SqlDataAdapter(cmd);

    DataSet ds = new DataSet();

    sda.Fill(ds);

    gvUsers.DataSource = ds;

    gvUsers.DataBind();

}

 

protected void OnSubmit(object sender, EventArgs e)

{

    string config = Convert.ToString(ConfigurationManager.ConnectionStrings["myconnection"]);

    SqlConnection con = new SqlConnection(config);

    con.Open();

    using (SqlCommand cmd = new SqlCommand("insert into users values(@Name,@Role)", con))

    {

        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;

        cmd.Parameters.Add("@Role", SqlDbType.VarChar).Value = ddlRole.SelectedItem.Text;

        cmd.ExecuteNonQuery();

    }

    con.Close();

    this.GetData();

  }

 



Note: All contents are copyright of their authors.



Comments



Leave a Reply

Comment:*
Email ID:*


Name:*