Cant get my grid to display the data from my sql

gussy81

New member
Joined
Apr 11, 2024
Messages
1
Programming Experience
Beginner
Hi i am struggling to get my gridview to return my data. nothing shows up. can anyone help?

my two pages are as follows:

ASP.net:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dashboard.aspx.cs" Inherits="WebApplication1.Dashboard" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Client Matters</title>

    <style type="text/css">
        .auto-style1 {
            width: 15px;
            height: 13px;
        }
        table, th, td {
  border:1px solid black;
    }

        tr:nth-child(odd) { background-color: FFF; }
tr:nth-child(even) { background-color: #fbd3d3; }
        </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <img alt="" class="auto-style1" src="file:///C:/Users/on.png" />User:
            <asp:Label ID="SessionLabel" runat="server" Text="Label"></asp:Label>
            <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
&nbsp;<asp:Button ID="ButtonLogout" runat="server" OnClick="ButtonLogout_Click" Text="Logout" />
            <asp:Button ID="matters" runat="server" OnClick="matters_Click" Text="Go to MTTR_Overall" />
            <br />
            <br />
            <br />
            <br />
            Welcome
            <asp:Label ID="lblFirstName" runat="server"></asp:Label>
            <asp:Label ID="lblSurName" runat="server"></asp:Label>
            &nbsp;to the Croskerrys Portal Page. Please check the below details are correct:<br />
            <br />
            Your Email Address:<asp:Label ID="lblEmail" runat="server"></asp:Label>
            &nbsp;and you reference number is&nbsp;&nbsp;&nbsp;
            <asp:Label ID="lblRefNum" runat="server"></asp:Label>
            &nbsp;- please reference this number on all correspondence with us.<br />
            <br />
            <br />
            <br />
            <br />
&nbsp;
            <br />
            <strong>&nbsp; </strong>
            <br />
            <br />
            <br />
            <br />
            <br />


        <!-- GridView to display matters -->
<asp:GridView ID="GridViewMatters" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridViewMatters_RowDataBound">
    <Columns>
        <asp:BoundField DataField="client_ref" HeaderText="Client Reference" />
        <asp:BoundField DataField="crosk_ref" HeaderText="Our Reference" />
        <asp:BoundField DataField="client_name" HeaderText="First Name" />
        <asp:BoundField DataField="section_name" HeaderText="Second Name" />
        <asp:BoundField DataField="curr_status" HeaderText="Status" />
        <asp:TemplateField HeaderText="Details">
            <ItemTemplate>
                <asp:HyperLink ID="lnkDetails" runat="server" Text="Details" NavigateUrl='<%# Eval("client_ref", "MD_Overall.aspx?id={0}") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
        </div>
</form>




</body>
</html>

and the c# page:

C#:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{


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

            if (Session["userId"] == null)
                Response.Redirect("Login.aspx");
            SessionLabel.Text = "Username : " + Session["userId"];

            SqlConnection myCon = new SqlConnection(ConfigurationManager.ConnectionStrings["croskConn"].ToString());

            try
            {
                string uid = Convert.ToString(Session["userId"]);

                myCon.Open();
                string qry = "select userId, FirstName, Surname, Email, ReferenceNum from users where userId='" + uid + "'";
                SqlCommand cmd = new SqlCommand(qry, myCon);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    string firstName = dt.Rows[0]["FirstName"].ToString();
                    lblFirstName.Text = firstName;
                    string surName = dt.Rows[0]["Surname"].ToString();
                    lblSurName.Text = surName;
                    string email = dt.Rows[0]["Email"].ToString();
                    lblEmail.Text = email;
                    string refNum = dt.Rows[0]["ReferenceNum"].ToString();
                    lblRefNum.Text = refNum;
                }
                else
                {
                    lblMsg.Text = "No record found!";
                }
                myCon.Close();

            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
                Response.Write(ex.Message);
            }

        }




        protected void ButtonLogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect("Login.aspx");

        }

        private void BindGridView()
        {
            string userId = Session["userId"].ToString();
            string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
            string query = "SELECT client_ref, crosk_ref, client_name, section_name, curr_status FROM md_overall_temp WHERE client_code = @userId";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@userId", userId);

                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable dt = new DataTable();
                adapter.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    GridViewMatters.DataSource = dt;
                    GridViewMatters.DataBind();
                }

            }
        }

        protected void GridViewMatters_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Add CSS class to alternate rows for better readability
                e.Row.CssClass = (e.Row.RowIndex % 2 == 0) ? "evenRow" : "oddRow";
            }
        }




        
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void matters_Click(object sender, EventArgs e)
        {
            matters.PostBackUrl = "https://localhost:44397/MD_overall.aspx";
        }

        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged2(object sender, EventArgs e)
        {

        }
    }
}
 
The first step is to verify that you are actually getting data back from the database. Have you stepped through your code with a debugger?
 
For future reference, please post only code relevant to the issue. The more irrelevant code you post, the harder it is for us to even find the problem, never mind focus on it. In your case, we definitely don't need the namespace declaration or anything outside it, and we definitely don't need empty event handlers. There may well be other irrelevant code there too and you should be able to make a decent fist of identifying it, because you should know what code you expect to affect loading data into your grid. You won't always get things exactly right but that's OK. Better to have to go back and forth every now and then than to just post all your code every time.
 

Latest posts

Back
Top Bottom