You must Sign In to post a response.
  • Category: ASP.NET

    Can not bind data on gridview

    my table has values.but i didn't get into my gridview...
    code is given below////
    con.Open();

    cmd = new SqlCommand();
    cmd.CommandText= "select * from gridview ";
    cmd.Connection = con;
    da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    gv_home.DataSource = ds;
    gv_home.DataBind();
    con.Close();
  • #767396
    Hi,

    Whether you got a data in your dataset or not please check it in debug mode and let us know, I guess data is missed in your dataset.

    Before bind it in gridview you just check whether dataset contain data or not.


    if(ds.Tables.Count >0 && ds.Tables[0].Rows.Count >0)
    {
    gv_home.DataSource=ds.Tables[0];
    gv_home.DataBind();
    }

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767404
    Hi,
    First check connection's status is 'Open' or not.
    Then try code given by Mr.naveensanagasetti or directly use DataTable as follows:

    DataTable dt = new DataTable();
    da.Fill(dt);
    gv_home.DataSource = dt;

  • #767409
    hi Krishna

    Aspx:


    Nameofthepost
    Description
    Qualification
    these are database column value

    <asp:GridView ID="cgv" runat="server" AutoGenerateColumns="False"
    CellSpacing="6" CssClass="style70" HeaderStyle-BackColor="#61A6F8"
    HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"
    OnPageIndexChanging="cgv_nextPage" OnRowCancelingEdit="cgv_RowCancelingEdit"
    OnRowDeleting="cgv_RowDeleting" OnRowEditing="cgv_RowEditing"
    OnRowUpdating="cgv_RowUpdating" Width="75%" >
    <Columns>
    <asp:TemplateField HeaderText="SNo.">
    <ItemTemplate>
    <%# Container.DataItemIndex + 1 %>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Id" Visible="false">
    <ItemTemplate>
    <asp:Label id="lblEditid" runat="server" Visible="false" Text='<%#Eval("id") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="NAME OF THE COURSE">
    <ItemTemplate>
    <asp:Label id="lbl1" runat="server" Font-Size="15px" Font-Bold="true" ForeColor="#0066ff" Text='<%#Bind("Nameofthepost") %>'></asp:Label> //
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="QUALIFICATION">
    <ItemTemplate>
    <asp:Label id="lbl2" runat="server" Font-Size="15px" Font-Bold="true" ForeColor="#0066ff" Text='<%#Bind("Qualification") %>'></asp:Label>
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="DESCRIPTION">
    <ItemTemplate>
    <asp:Label id="lbl3" runat="server" Font-Size="15px" Font-Bold="true" ForeColor="BLACK" Text='<%#Bind("Description") %>'></asp:Label>
    </ItemTemplate>

    </asp:TemplateField>

    </columns>
    </asp:gridView>


    C#


    private void BindGridView()
    {
    try
    {
    SqlConnection con = Connection.DBconnection();
    SqlCommand command = new SqlCommand("SELECT * from tablename", con);
    SqlDataAdapter da = new SqlDataAdapter(command);
    DataTable dt = new DataTable();
    da.Fill(dt);
    gridview.DataSource = dt;
    gridview.DataBind();
    }
    catch (Exception ex)
    {

    }
    }

    hope this will help you
    Paul.S

  • #767432
    Hi

    you can try this code working good





    <form id="form1" runat="server">
    <asp:GridView ID="Grd1" runat="server">
    </asp:GridView>
    </form>



    SqlConnection sqlcon = new SqlConnection("Data Source=JESUS-PC;Initial Catalog=userdb;Integrated Security=True;");
    protected void Page_Load(object sender, EventArgs e)
    {
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "select * from mas";
    cmd.Connection = sqlcon;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    Grd1.DataSource = ds;
    Grd1.DataBind();
    sqlcon.Close();


    }


    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #767433
    check first if your dataset has values, if it is empty then you gridview may not get bind, additionally you can set TRY...CATCH to check if there is any error occurred while binding data to gridview, have you put row_databound event in your code ? if yes then you need to check it also.
    I think debugger is the good way to resolve your problem

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767440
    1. Make sure you are getting the records from the database.
    2. Following are the main syntax for databiding to gridview

    MyGridGrd1.DataSource = YourDataSource;
    MyGridGrd1.DataBind();

    3. Even Though you assign the data source. In ASPX page is important.
    a. Check the Gridview is visible
    b. Check AutoGenerateColumns="true"
    c. if AutoGenerateColumns="false" you have to bind the values manually in the gridview.

    By Nathan
    Direction is important than speed


  • Sign In to post your comments