[英]I need to Show the data from database into my gridview on aspx webform page
I want to fetch the data from the sql server database and show it into the gridview which i have created on my aspx webform page but it's not fetching it instead it's throwing an error that says **A field or property with the name 'Name' was not found on the selected data source. 我想从sql server数据库中获取数据并将其显示到我在aspx Webform页面上创建的gridview中,但它没有获取它,而是抛出了一个错误,提示“名称或名称为**的字段或属性”在所选数据源上找不到。 ** I already have a data row into my database with values under column name and i have created gridview columns in my code but that doesn't seem to work.Please help me get through with this problem.Below is my whole code:
**我已经在数据库中有一个数据行,其中的列名下有值,并且我已经在代码中创建了gridview列,但这似乎不起作用。请帮助我解决这个问题。以下是我的整个代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> Welcome To My First Web Form<br /> <h1> Candidate Registration Form</h1> <br /> <br /> Applicant's Name: <asp:TextBox ID="TextBox1" runat="server" CausesValidation="True" EnableViewState="False" ValidateRequestMode="Enabled"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Please enter Your Name!" ForeColor="Red"></asp:RequiredFieldValidator> <br /> <br /> <asp:Label ID="Label1" runat="server" Text="Applicant's FName"></asp:Label> : <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="Please enter your Father name!" ForeColor="Red"></asp:RequiredFieldValidator> <br /> <br /> <asp:Label ID="Label3" runat="server" Text="Gender"></asp:Label>: <asp:RadioButtonList ID="gender" RepeatDirection="Horizontal" runat="server" Width="141px" RepeatLayout="Flow"> <asp:ListItem>Male</asp:ListItem> <asp:ListItem>Female</asp:ListItem> </asp:RadioButtonList> <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="gender" ErrorMessage="Please Choose your Gender" ForeColor="Red"></asp:RequiredFieldValidator> <br /> <br /> <asp:Label ID="Label2" runat="server" Text="E-mail ID"></asp:Label> : <asp:TextBox ID="TextBox3" runat="server" OnTextChanged="TextBox3_TextChanged"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox3" Display="Dynamic" ErrorMessage="E-mail address is required">*</asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox3" Display="Dynamic" ErrorMessage="E-mail addresses must be in the format of name@domain.xyz" ValidationExpression="\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*" ForeColor="Red">Invalid Format</asp:RegularExpressionValidator> <br /> <br /> <asp:Label ID="Label4" runat="server" Text="password"></asp:Label> : <asp:TextBox ID="TextBox4" runat="server" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="TextBox4" Display="Dynamic" ErrorMessage="Password is Required!" ForeColor="Red"></asp:RequiredFieldValidator> <br /> <br /> <asp:Label ID="Label5" runat="server" Text="Confirm Password"></asp:Label> : <asp:TextBox ID="TextBox5" runat="server" TextMode="Password"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox5" ErrorMessage="The passwords Didn't Match!" ForeColor="Red" ControlToCompare="TextBox4" Display="Dynamic"></asp:CompareValidator> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /> <br /> <asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"> <Columns> <asp:BoundField DataField="Name" /> <asp:BoundField DataField="FatherName" /> <asp:BoundField DataField="Email" /> </Columns> </asp:GridView> <br /> <br /> <br /> </div> </form> </body> </html>
Below is the code for cs file behind: 以下是背后的cs文件的代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace WebApplication5 { public partial class WebForm1 : System.Web.UI.Page { SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\admin\\documents\\visual studio 2013\\Projects\\WebApplication5\\WebApplication5\\App_Data\\Candidates.mdf;Integrated Security=True"); public static int count = 0; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { gvbind(); //DataTable dt = new DataTable(); //dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Name"), new DataColumn("FatherName"), new DataColumn("E-mail") }); //ViewState["Candidates"] = dt; //GridView1.DataSource = (DataTable)ViewState["Candidates"]; //GridView1.DataBind(); } } protected void gvbind() { con.Open(); SqlCommand cmd = new SqlCommand("Select * from Candidates", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); } protected void TextBox3_TextChanged(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { count += 1; int rowIndex = 0; con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into Candidates values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"')"; cmd.ExecuteNonQuery(); con.Close(); //if (count > 1) //{ // DataTable dtCurrentTable = (DataTable)ViewState["Candidates"]; // DataRow drCurrentRow = null; // if (dtCurrentTable.Rows.Count > 0) { // //for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) // //{ // drCurrentRow = dtCurrentTable.NewRow(); // int totalrows = dtCurrentTable.Rows.Count; // dtCurrentTable.Rows.Add(drCurrentRow); // dtCurrentTable.Rows[totalrows]["Name"] = TextBox1.Text; // dtCurrentTable.Rows[totalrows]["FatherName"] = TextBox2.Text; // dtCurrentTable.Rows[totalrows]["E-mail"] = TextBox3.Text; // rowIndex++; // //} // ViewState["Candidates"] = dtCurrentTable; // GridView1.DataSource = dtCurrentTable; // GridView1.DataBind(); // TextBox1.Text = string.Empty; // TextBox2.Text = string.Empty; // TextBox3.Text = string.Empty; // } //} //else //{ // DataTable dt = (DataTable)ViewState["Candidates"]; // dt.Rows.Add(TextBox1.Text.Trim(), TextBox2.Text.Trim(), TextBox3.Text.Trim()); // ViewState["Candidates"] = dt; // TextBox1.Text = string.Empty; // TextBox2.Text = string.Empty; // TextBox3.Text = string.Empty; // BindGrid(); //} } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } //protected void BindGrid() //{ // //GridView1.DataSource = (DataTable)ViewState["Candidates"]; // //GridView1.DataBind(); //} } }
Just ignore those commented codes,and that error which i mentioned comes when i debug the code on line Gridview1.DataBind();
只需忽略那些注释的代码,当我在
Gridview1.DataBind();
行上调试代码时,就会出现我提到的错误Gridview1.DataBind();
in method gvbind(). 在方法gvbind()中。
<asp:BoundField DataField="Cname" />
<asp:BoundField DataField="Cfname" />
<asp:BoundField DataField="Cmail" />
this will work 这会工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.