locked
data no fill to the textbox error occur non invocable member tbl row cells cannot be us like a method? RRS feed

  • Question

  • User-1026236167 posted

    hello here in my code i have an error occur non invocable member tablerow cells cannnot be use like a method  when i 
    enter some data in the textbox and click the addinlist button and this display grdview row when click on edit button
    in gridview row want to fill the related row values to the textbox this is my requirement


    ASPX <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="demo.aspx.cs" Inherits="WebApplication14.demo" %> <asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server"> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:HiddenField ID="HiddenField1" runat="server" /> <div class="container-fluid"> <div class="row"> <div class="col-sm-2"> <label>Name </label> <asp:TextBox ID="TB_Name" runat="server" CssClass="form-control" AutoCompleteType="Disabled" /> </div> <div class="col-sm-2"> <label>Class </label> <asp:TextBox ID="TB_class" runat="server" CssClass="form-control" AutoCompleteType="Disabled" /> </div> <div class="col-sm-2"> <label>Roll No </label> <asp:TextBox ID="TB_rollno" runat="server" CssClass="form-control" AutoCompleteType="Disabled" /> </div> <div class="col-sm-2"> <label>U I </label> <asp:TextBox ID="TB_ui" runat="server" CssClass="form-control" AutoCompleteType="Disabled" /> </div> <div class="col-sm-2"> <br /> <asp:Button ID="Button1" runat="server" Text="Add In List" CssClass="btn btn-primary" OnClick="Button1_Click" /> </div> <div class="col-sm-2"> <br /> <asp:Button ID="Save" runat="server" Text="Save" CssClass="btn btn-primary" OnClick="Save_Click" /> </div> </div> <asp:GridView ID="Gv1" runat="server" AutoGenerateColumns="false" Width="100%" DataKeyNames="name, class, roll_no, tbl_id" EmptyDataText="No Products Added." CssClass="table table-striped table-bordered table-hover" HeaderStyle-BackColor="#0063a4" HeaderStyle-ForeColor="White" > <Columns> <asp:TemplateField HeaderText="Sr. No."> <ItemTemplate> <%# Container.DataItemIndex+1 %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <%-- <%# Eval("name") %><br />--%> <asp:Label runat="server" ID="name" Text='<%# Eval("name") %>'></asp:Label> <asp:Button ID="BT_delete" runat="server" Text="X Remove" OnClick="BT_delete_Click" CssClass="btn btn-danger" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Class"> <ItemTemplate> <%-- <%# Eval("class") %>--%> <asp:Label runat="server" ID="class" Text='<%# Eval("class") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Roll No"> <ItemTemplate> <%-- <%# Eval("roll_no") %><br />--%> <asp:Label runat="server" ID="rollno" Text='<%# Eval("roll_no") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="Button1" runat="server" Text="Edit" CssClass="btn btn-danger" OnClick="Button2_Click"></asp:LinkButton> <asp:Button ID="BT_update" runat="server" Text="Update" style="border-radius:5px; margin-left:1.5em" Height="35px" Width="80px" OnClick="BT_update_Click" BackColor="#3366CC" BorderColor="#3366CC" ForeColor="White" /> </ItemTemplate> </asp:TemplateField> <%-- <asp:TemplateField HeaderText="Roll No"> <ItemTemplate> <%# Eval("invoice") %><br /> </ItemTemplate> </asp:TemplateField>--%> </Columns> </asp:GridView> </div> </ContentTemplate> </asp:UpdatePanel> </asp:Content> 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 WebApplication14 { public partial class demo : System.Web.UI.Page { SqlCommand cmd = new SqlCommand(); SqlConnection con = new SqlConnection(); string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString(); public void EstablishConnection(string storeprocedure) { con.ConnectionString = connection; cmd.Connection = con; cmd.Connection.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = storeprocedure; } public void CloseConnection() { cmd.Connection.Close(); cmd.Connection.Dispose(); con.Close(); } public void FillInformation(Guid invoice_id) { using (hospitalEntities ctx = new hospitalEntities()) { var cust = (from a in ctx.tbl_demo1 where a.invoice == invoice_id select a).FirstOrDefault(); if (cust != null) { TB_Name.Text = cust.name; TB_class.Text = cust.@class; TB_rollno.Text = cust.roll_no; } } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Passviewstate(); if (Request.QueryString["id"] != null) { //Guid invoice_id = new Guid(Request.QueryString["id"].ToString()); Guid invoice_id = new Guid (Request.QueryString["id"].ToString()); FillInformation(invoice_id); SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_demo1 where tbl_demo1.invoice='" + invoice_id.ToString() + "'", connection); DataTable DT = new DataTable(); adp.Fill(DT); Gv1.DataSource = DT; Gv1.DataBind(); DataTable stu = ViewState["stu1"] as DataTable; stu = DT; ViewState["stu1"] = stu; } } } protected void Button2_Click(object sender, EventArgs e) { if (ViewState["stu1"] != null) { if (Request.QueryString["id"] == null) { LinkButton btn = (LinkButton)sender; GridViewRow gvr = (GridViewRow)(btn.Parent.Parent); DataTable stu = ViewState["stu1"] as DataTable; if (stu.Rows.Count > 0) { //DataBoundLiteralControl lit = gvr.Cells[0].Controls[0] as DataBoundLiteralControl; //TB_Name.Text = lit.Text; ////DataBoundLiteralControl lit2 = gvr.Cells[1].Controls[0] as DataBoundLiteralControl; ////DataBoundLiteralControl lit3 = gvr.Cells[2].Controls[0] as DataBoundLiteralControl; //TB_class.Text = lit2.Text; //TB_rollno.Text = lit3.Text; TB_Name.Text = stu.Rows[(int) gvr.Cells(0)].ToString(); TB_class.Text = stu.Rows[(int) gvr.Cells(1)].ToString(); TB_rollno.Text = stu.Rows[(int)gvr.Cells(2)].ToString(); HiddenField1.Value = Gv1.DataKeys[gvr.RowIndex].Values[0].ToString(); ViewState["stu1"] = stu as DataTable; //Button2.Visible = false; //Button3.Visible = true; } } } } protected void BT_delete_Click(object sender, EventArgs e) { if (ViewState["stu1"] != null) { if (Request.QueryString["id"] == null) { Button link = (Button)sender; GridViewRow row = (GridViewRow)(link.Parent.Parent); DataTable stu = ViewState["stu1"] as DataTable; if (stu.Rows.Count > 0) { stu.Rows[row.RowIndex].Delete(); stu.AcceptChanges(); ViewState["stu1"] = stu as DataTable; Gv1.DataSource = stu; Gv1.DataBind(); } } } if (ViewState["stu1"] != null) { if (Request.QueryString["id"] != null) { Button link = (Button)sender; GridViewRow row = (GridViewRow)(link.Parent.Parent); DataTable stu = ViewState["stu1"] as DataTable; Guid invoice_id = new Guid(Request.QueryString["id"].ToString()); if (stu.Rows.Count > 0) { stu.Rows[row.RowIndex].Delete(); stu.AcceptChanges(); ViewState["stu1"] = stu as DataTable; using (hospitalEntities ctx = new hospitalEntities()) { tbl_demo1 customer = (from c in ctx.tbl_demo1 where c.invoice == invoice_id select c).FirstOrDefault(); ctx.Entry(customer).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } Gv1.DataSource = stu; Gv1.DataBind(); } } } } public void Passviewstate() { DataTable stu = new DataTable(); stu.Columns.Add("name"); stu.Columns.Add("class"); stu.Columns.Add("roll_no"); stu.Columns.Add("tbl_id"); ViewState["stu1"] = stu as DataTable; } protected void BT_update_Click(object sender, EventArgs e) { decimal Tbl_id = Convert.ToDecimal(HiddenField1.Value); using (hospitalEntities ctx = new hospitalEntities()) { var package = (from c in ctx.tbl_demo1 where c.tbl_id == Tbl_id select c).FirstOrDefault(); package.name = TB_Name.Text; package.@class = TB_class.Text; package.roll_no = TB_rollno.Text; ctx.SaveChanges(); } // Fillgridview(); } protected void Button1_Click(object sender, EventArgs e) { if (ViewState["stu1"] != null) { if (Request.QueryString["id"] == null) { DataTable DT = ViewState["stu1"] as DataTable; DT.Rows.Add(TB_Name.Text, TB_class.Text, TB_rollno.Text); Gv1.DataSource = DT; Gv1.DataBind(); } } if (ViewState["stu1"] != null) { if (Request.QueryString["id"] != null) { DataTable stu = ViewState["stu1"] as DataTable; Guid invoice_id = new Guid(Request.QueryString["id"].ToString()); stu.Rows.Add(TB_Name.Text, TB_class.Text, TB_rollno.Text); ViewState["stu1"] = stu as DataTable; Gv1.DataSource = stu; Gv1.DataBind(); } } } protected void Save_Click(object sender, EventArgs e) { //if (ViewState["stu1"] != null) //{ // DataTable DT1 = ViewState["stu1"] as DataTable; // DT1.Rows.Add(TB_Name.Text, TB_class.Text, TB_rollno.Text); // ViewState["stu1"] = DT1; // Gv1.DataSource = DT1; //} int status = 0; Guid invoiceid = Guid.NewGuid(); foreach (GridViewRow row in Gv1.Rows) { using (hospitalEntities ctx = new hospitalEntities()) { ctx.tbl_demo1.Add(new tbl_demo1() { name = (Gv1.DataKeys[row.RowIndex].Values["name"].ToString()), @class = (Gv1.DataKeys[row.RowIndex].Values["class"].ToString()), roll_no = (Gv1.DataKeys[row.RowIndex].Values["roll_no"].ToString()), invoice = invoiceid, }); status = ctx.SaveChanges(); } } } } }

    Thursday, October 22, 2020 8:56 AM

All replies

  • User-189459990 posted

    To access the items of cell collection, you have to use the indexer, which is done with square brackets ([]), rather than round brackets (()).

    So it should be

    gvr.Cells[index]

    Besides, if you want to get a specific cell value, you can refer to the following demo.

    When using <asp:TemplateFields>, you can't access the cell.Text directlly. Actually you need to get the text of <asp:Label> which is inside <asp:TemplateFields>.

    TB_Name.Text = ((Label)gvr.Cells[0].FindControl("name")).Text;
    TB_class.Text = ((Label)gvr.Cells[1].FindControl("class")).Text;
    TB_rollno.Text = ((Label)gvr.Cells[2].FindControl("rollno")).Text;

    Friday, October 23, 2020 5:11 AM