Question

Here, I have Attendance table having attributes such as

Status - bit, 
StudId - bigint (foreign key), 
AttendanceId - int =>primary key , identity(true) 

and student table having

StudID - bigint =>primary key, 
StudFirstName - varchar(50), 
StudLastName - varchar(50) 

To be displayed in gridview. Here is the code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Class_Content_AddAttendancesss : System.Web.UI.Page
{
DataClassesDataContext db = new DataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
    var cn = from p in db.Attendances
             where (p.BranchId == Convert.ToInt32(DropDownList1.SelectedValue))
                  && (p.SemesterId == Convert.ToInt32(DropDownList2.SelectedValue))
             select new
             {
                 p.AttendanceId,
                 p.Status,
                 p.StudID,
                 p.Student.StudFirstName,
                 p.Student.StudLastName,
                 p.BranchId,
                 p.SemesterId,
                 p.Branch.BranchName,
                 p.Semester.SemesterName

             };
    GridView1.DataSource = cn;
    GridView1.DataBind();
}
}

And in design :

            <asp:Label ID="Label1" runat="server" Text='<%#Eval("StudId") %>'></asp:Label>
            <asp:Label ID="Label2" runat="server" Text='<%#Eval("StudFirstName") %>'></asp:Label>
            <asp:Label ID="Label3" runat="server" Text='<%#Eval("StudLastName") %>'></asp:Label>
           <asp:CheckBox ID="CheckBox1" Text='<%#Eval("Status") %>' runat="server" /> 
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>
Was it helpful?

Solution

Assing data as

 Text='<%# Eval("StudId").ToString() %>'

 Text='<%# Eval("Status").ToString() %>'

or specify the gridview columns as Integer type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top