Я видел эти вопросы раньше на других сайтах, но не могу найти решение. Я только что создал простое представление данных, используя вид сетки
Webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CusDB.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Id" ForeColor="#333333" GridLines="None" ShowFooter="True" OnRowCommand="GridView1_RowCommand" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="CusId">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CusId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CusId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CusId" SortExpression="CusId">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CusId") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCusId" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("CusId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mail" SortExpression="Mail">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Mail") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMail" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Mail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="Password">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Password") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("City") %>'>
<asp:ListItem>Select City</asp:ListItem>
<asp:ListItem>Bangalore</asp:ListItem>
<asp:ListItem>Chennai</asp:ListItem>
<asp:ListItem>Hyderabad</asp:ListItem>
<asp:ListItem>Lucknow</asp:ListItem>
<asp:ListItem>New Delhi</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="txtDrop" runat="server">
<asp:ListItem>Select City</asp:ListItem>
<asp:ListItem>Bangalore</asp:ListItem>
<asp:ListItem>Chennai</asp:ListItem>
<asp:ListItem>Hyderabad</asp:ListItem>
<asp:ListItem>Lucknow</asp:ListItem>
<asp:ListItem>New Delhi</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<EditItemTemplate>
<asp:Button ID="txtUpdate" CommandArgument='<%# Eval("CusId") %>' CommandName='UpdateRow' runat="server" Text="Update" />
<asp:Button ID="txtCancel" CommandArgument='<%# Eval("CusId") %>' CommandName='CancelRow' runat="server" Text="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="txtInsert" runat="server" Text="Insert" />
</FooterTemplate>
<ItemTemplate>
<asp:Button ID="txtEdit" CommandArgument='<%# Eval("CusId") %>' CommandName='EditRow' runat="server" Text="Edit" />
<asp:Button ID="txtDelete" CommandArgument='<%# Eval("CusId") %>' CommandName='DeleteRow' runat="server" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</form>
</body>
</html>
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CusDB
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridDate();
}
}
private void BindGridDate()
{
GridView1.DataSource = Details.GetAlldetails();
GridView1.DataBind(); **//Getting error here**
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditRow")
{
int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
GridView1.EditIndex = rowIndex;
BindGridDate();
}
else if (e.CommandName == "DeleteRow")
{
Details.DeleteCustomer(Convert.ToInt32(e.CommandArgument));
BindGridDate();
}
else if (e.CommandName == "CancelRow")
{
GridView1.EditIndex = -1;
BindGridDate();
}
else if (e.CommandName == "UpdateRow")
{
int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
int cusId = Convert.ToInt32(e.CommandArgument);
string name = ((TextBox)GridView1.Rows[rowIndex].FindControl("TextBox2")).Text;
string mail = ((TextBox)GridView1.Rows[rowIndex].FindControl("TextBox3")).Text;
string password = ((TextBox)GridView1.Rows[rowIndex].FindControl("TextBox4")).Text;
string city = ((DropDownList)GridView1.Rows[rowIndex].FindControl("DropDownList1")).SelectedValue;
Details.UpdateCustomer(cusId, name, mail, password, city);
GridView1.EditIndex = -1;
BindGridDate();
}
else if (e.CommandName == "InsertRow")
{
string name = ((TextBox)GridView1.FooterRow.FindControl("txtName")).Text;
string mail = ((TextBox)GridView1.FooterRow.FindControl("txtMail")).Text;
string password = ((TextBox)GridView1.FooterRow.FindControl("txtPassword")).Text;
string city = ((DropDownList)GridView1.FooterRow.FindControl("txtDrop")).SelectedValue;
Details.InsertCustomer(name, mail, password, city);
BindGridDate();
}
}
}
}
ОШИБКА: привязка данных : «CusDB.Customer» не содержит свойства с именем «Id».
Я пытался изменить Eval Id на Eval CusId
Details.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace CusDB
{
public class Customer
{
public int CusId{ get; set; }
public string Name{ get; set;}
public string Mail{get; set;}
public string Password{get; set;}
public string City{get; set;}
}
public class Details
{
public static List<Customer> GetAlldetails()
{
List<Customer> listdetails = new List<Customer>();
string CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("select * from Final", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Customer customer = new Customer();
customer.CusId = Convert.ToInt32(rdr["CusId"]);
customer.Name = rdr["Name"].ToString();
customer.Mail = rdr["Mail"].ToString();
customer.Password = rdr["Password"].ToString();
customer.City = rdr["City"].ToString();
listdetails.Add(customer);
}
}
return listdetails;
}
public static void DeleteCustomer(int CusId)
{
string CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("delete from Final where CusId = @CusId", con);
SqlParameter param = new SqlParameter("@CusId", CusId);
cmd.Parameters.Add(param);
con.Open();
cmd.ExecuteNonQuery();
}
}
public static int UpdateCustomer(int CusId, string Name, string Mail, string Password, string City)
{
string CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
string UpdateQuery = "update Final set Name = @Name, " + "Mail = @Mail, Password = @Password, City = @City where CusId = @CusId";
SqlCommand cmd = new SqlCommand(UpdateQuery, con);
SqlParameter paramId = new SqlParameter("@CusId", CusId);
cmd.Parameters.Add(paramId);
SqlParameter paramName = new SqlParameter("@Name", Name);
cmd.Parameters.Add(paramName);
SqlParameter paramMail = new SqlParameter("@Mail", Mail);
cmd.Parameters.Add(paramMail);
SqlParameter paramPassword = new SqlParameter("@Password", Password);
cmd.Parameters.Add(paramPassword);
SqlParameter paramCity = new SqlParameter("@City", City);
cmd.Parameters.Add(paramCity);
con.Open();
return cmd.ExecuteNonQuery();
}
}
public static int InsertCustomer(string Name, string Mail, string Password, string City)
{
string CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
string updateQuery = "insert into Final (Name, Mail, Password, City)" + "values (@Name, @Mail, @Password, @City)";
SqlCommand cmd = new SqlCommand(updateQuery, con);
SqlParameter paramName = new SqlParameter("@Name", Name);
cmd.Parameters.Add(paramName);
SqlParameter paramMail = new SqlParameter("@Mail", Mail);
cmd.Parameters.Add(paramMail);
SqlParameter paramPassword = new SqlParameter("@Password", Password);
cmd.Parameters.Add(paramPassword);
SqlParameter paramCity = new SqlParameter("@City", City);
cmd.Parameters.Add(paramCity);
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
}
SQL Это моя SQL Таблица
CREATE TABLE [dbo].[Final] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[CusId] INT NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Mail] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[City] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);