Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
info.aspx.cs
C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            /*String name;
            String address;
            String department;
            if (gender.SelectedItem.Value == "Male")
             {
                  name = nameTextBox.Text.Trim();
                  address = addressTextBox.Text.Trim();
                  department=showDropDownList.Text.Trim();
                 String query="insert into info (Name,Address,Gender,Department) values ('" + name + "','" + address + "','Male','" + department + "')";
                 DataAccess.ExecuteSQL(query);
             }

             else if (gender.SelectedItem.Value == "Female")
             {
                
                name = nameTextBox.Text.Trim();
                address = addressTextBox.Text.Trim();
                department=showDropDownList.Text.Trim();
                 String query = "insert into info (Name,Address,Gender,Department) values ('" + name + "','" + address + "','Female','" + department + "')";
                 DataAccess.ExecuteSQL(query);
             }*/
            String sql = "SELECT Name, Address, Gender, Department FROM  info ";
            DataTable t = DataAccess.GetDataTable(sql);
            this.GridView1.Visible = true;
            GridView1.DataSource = t;
            GridView1.DataBind();
            /*nameTextBox.Text = "";
            addressTextBox.Text = "";
            gender.ClearSelection(); */
        }
    }
}


info.aspx

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="info.aspx.cs" Inherits="WebApplication1.info" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <br />
        <br />
        <asp:Label ID="nameLabel" runat="server" Text="Name"></asp:Label>
        <asp:TextBox ID="nameTextBox" runat="server" Height="28px"
            style="margin-left: 81px" Width="221px"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="addressLabel" runat="server" Text="Address"></asp:Label>
        <asp:TextBox ID="addressTextBox" runat="server" style="margin-left: 66px"
            Width="225px" Height="28px" ></asp:TextBox>
        <br />
        <br />
        <table style="width:100%;">
            <tr>
                <td><asp:Label ID="genderLabel" runat="server" Text="Gender"></asp:Label> </td>
                <td> <asp:RadioButtonList ID="gender" runat="server" Height="24px"
            Width="190px" style="margin-left: 81px; margin-top: 0px;">
            <asp:ListItem>Male</asp:ListItem>
            <asp:ListItem>Female</asp:ListItem>
        </asp:RadioButtonList></td>
            </tr>


        </table>
        <br />
        <asp:Label ID="deptLabel" runat="server" Text="Department"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:DropDownList ID="showDropDownList" runat="server">
            <asp:ListItem>BBA</asp:ListItem>
            <asp:ListItem>CSE</asp:ListItem>
            <asp:ListItem>EEE</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Show" />


        <br />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            style="margin-top: 0px" Width="529px">
        </asp:GridView>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />


    </div>
    </form>
</body>
</html>
Posted
v2
Comments
bbirajdar 26-Oct-13 0:19am    
Bind it in the pageload

change your gridview property
set this property

AutoGenerateColumns="True"
 
Share this answer
 
Hii,,, Friend.
may be you get some help from this...
There are two ways to solve this problem...

(1) use this gridview :
XML
   <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False"
style="margin-top: 0px" Width="529px">
        <Columns>
         <asp:BoundField DataField ="Name" HeaderText ="Name" ReadOnly="true" />
         <asp:BoundField DataField ="Address" HeaderText ="Address" ReadOnly="true"/>
         <asp:BoundField DataField ="Gender " HeaderText ="Gender " ReadOnly="true"/>
         <asp:BoundField DataField ="Department" HeaderText ="Department" ReadOnly="true"/>
        </Columns>
    </asp:GridView>


(2) Plz change this in your code AutoGenerateColumns="False", to
AutoGenerateColumns="True"...
then you get the data in the grid view....
like this..
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
            style="margin-top: 0px" Width="529px">
        </asp:GridView>


Also keep in mind you have to Bind the DataSource to GridView in Code Behind.......

also you Bind the data in the Page Load..

Hopefully it Works..
Best of Luck...
 
Share this answer
 
v4
use this grid view

C#
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False"
    Width="100%">
           <Columns>
            <asp:BoundField DataField ="Name" HeaderText ="Name" ReadOnly="true" />
            <asp:BoundField DataField ="Address" HeaderText ="Address" ReadOnly="true"/>
            <asp:BoundField DataField ="Gender " HeaderText ="Gender " ReadOnly="true"/>
            <asp:BoundField DataField ="Department" HeaderText ="Department" ReadOnly="true"/>
           </Columns>
       </asp:GridView>


OR

set autogenratedcolumn =true in your code
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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