Please Help Me For This Bug

Last post 05-09-2008 10:02 AM by Raggers. 2 replies.

Sort Posts:

  • Please Help Me For This Bug

    05-09-2008, 9:25 AM
    • Loading...
    • Maraqa84
    • Joined on 04-04-2008, 10:15 AM
    • Posts 1

     I have This Bug  Whenever i Try To filter Gridview With this Drop Down List

     

    An error has occurred because a control with id 'GridView1$ctl05$ctl00' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.

     

    This is my Code Snippets

    ViewEmployeeInfo.aspx
    -------------------------------------- 

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewEmployeesInfo.aspx.cs" Inherits="Admin_ViewEmployeesInfo" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            &nbsp; &nbsp;&nbsp;
         <asp:GridView ID="GridView1" runat="server"

    OnRowDataBound="Employeee_RowDataBound"  AllowPaging="true" PageIndex="5" AutoGenerateColumns="false"  OnPageIndexChanging="GridView_PageIndexChanging"   OnRowDeleting="GridView_RowDeleting" style="z-index: 101; left: 277px; position: absolute; top: 144px">
            </asp:GridView>
            <asp:DropDownList ID="BranchNameddl" runat="server" AutoPostBack="true" Style="z-index: 103; left: 289px;
                position: absolute; top: 100px" Width="157px">
            </asp:DropDownList>
         
        </div>
        </form>
    </body>
    </html>

     

    ViewEmployeeInfo.cs
    ------------------------------------ 

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;

    public partial class Admin_ViewEmployeesInfo : System.Web.UI.Page
    {
        private DataTable Store = new DataTable();
        private DataRow dr;
        private String sqlQuery="";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
          {
              populateBranchNameDDL();
              sqlQuery = "Select * from Cashierman where Roles = '1'";
             
                Store.Columns.Add(new DataColumn("UserName", typeof(String)));
                Store.Columns.Add(new DataColumn("FName", typeof(String)));
                Store.Columns.Add(new DataColumn("LName", typeof(String)));
                Store.Columns.Add(new DataColumn("BranchName", typeof(String)));
                Store.Columns.Add(new DataColumn("Cashier", typeof(int)));


                BoundField bf11 = new BoundField();

                bf11.HeaderText = "Edit";
                GridView1.Columns.Add(bf11);
             
                    BoundField bf0 = new BoundField();

                    bf0.DataField = "UserName";
                    bf0.HeaderText = "UserName";
                        GridView1.Columns.Add(bf0);

                        BoundField bf1 = new BoundField();

                        bf1.DataField = "FName";
                        bf1.HeaderText = "F.Name";
                        GridView1.Columns.Add(bf1);

                        BoundField bf2 = new BoundField();

                        bf2.DataField = "LName";
                        bf2.HeaderText = "L.Name";
                        GridView1.Columns.Add(bf2);

                        BoundField bf3 = new BoundField();

                        bf3.DataField = "BranchName";
                        bf3.HeaderText = "Branch";
                        GridView1.Columns.Add(bf3);

                        BoundField bf4 = new BoundField();

                        bf4.DataField = "Cashier";
                        bf4.HeaderText = "Cashier#";
                        GridView1.Columns.Add(bf4);
             
               
             
              GridView1.DataSource = Store;
                GridView1.DataBind();

               fillEmployeesGridView();

               Session["EmployeeGridView"] = Store;
             }

             if (IsPostBack) {

                 sqlQuery = "Select * from Cashierman where BranchName ='" + BranchNameddl.SelectedItem.Text + "' and Roles=1";

                 if (BranchNameddl.SelectedValue == "All")
                 {
                     sqlQuery = "Select * from Cashierman where Roles = '1'";

                 }


                Store = (DataTable)Session["EmployeeGridView"];
               Store.Rows.Clear();
                fillEmployeesGridView();
           //      GridView1.DataSource = Store;
             //    GridView1.DataBind();
                
             }

        }

     

        public void fillEmployeesGridView() {

            SqlConnection conn;
            SqlCommand cmd;
            string cmdString = "select * from Cashierman";
        

            conn = new SqlConnection("Data Source=MARAQA\\SQLEXPRESS;Initial Catalog=MarketTest1;Integrated Security=True;Pooling=False");
            cmd = new SqlCommand(sqlQuery, conn);
            conn.Open();

            SqlDataReader myReader;


            myReader = cmd.ExecuteReader();

            while (myReader.Read())
            {
               

                  dr = Store.NewRow();
                  dr[0] = myReader.GetString(0);
                  dr[1] = myReader.GetString(2);
                  dr[2] = myReader.GetString(3);
                  dr[4] = myReader.GetInt32(4);
                  dr[3] = myReader.GetString(5);

                 Store.Rows.Add(dr);

           
                
            }

            GridView1.DataSource = Store;
            GridView1.DataBind();


        
            conn.Close();

        }


        protected  void Employeee_RowDataBound(object sender,GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink hyperlink = new HyperLink();

              //  selectButton.ID = "linkbtn";
              //  selectButton.ImageUrl = "~/images/update.gif";
              //  selectButton.Click += new ImageClickEventHandler(select_button_Click);
                
               // selectButton.
               // dr.Table.Columns[0].ToString();
                //   hyperlink.NavigateUrl = "~/Admin/AddNewItem.aspx?"+e.Row.Cells[1].Text;
                // hyperlink.Text = "Select";
              
            ImageButton updateButton = new ImageButton();
            updateButton.PostBackUrl="~/Admin/UpdateEmployee.aspx?"+e.Row.Cells[1].Text;
            updateButton.ImageUrl = "~/images/edit.gif";
            e.Row.Cells[0].Controls.Add(updateButton);

            ImageButton deleteButton = new ImageButton();
            deleteButton.ID = "deletebtn";


           // deleteButton.PostBackUrl = "~/Admin/ViewEmployeesInfo.aspx";
            deleteButton.CommandArgument = e.Row.Cells[1].Text;
            deleteButton.ImageUrl = "~/images/delete.gif";
            deleteButton.CommandName = "Delete";
            deleteButton.OnClientClick = "return confirm('Are you sure to delete the record?')";
                          
            e.Row.Cells[0].Controls.Add(deleteButton);
          
           
            }
        }

        protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //CreateTemplatedGridView();
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

        protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

         //   lbl1.Text = GridView1.Rows[e.RowIndex].Cells[1].Text;
            string rowtodelete = GridView1.Rows[e.RowIndex].Cells[1].Text; ; 
            SqlConnection conn;
            SqlCommand cmd;

            string cmdString = "Delete From Cashierman where EmployeeUserName='" + rowtodelete + "'";

            try
            {
                conn = new SqlConnection("Data Source=MARAQA\\SQLEXPRESS;Initial Catalog=MarketTest1;Integrated Security=True;Pooling=False");
                cmd = new SqlCommand(cmdString, conn);
                conn.Open();

                int val = cmd.ExecuteNonQuery();



                conn.Close();

              //  fillEmployeesGridView();

                //Session["EmployeeGridView"] = Store;
                //GridView1.DataSource = Store;
               GridView1.DataBind();
                Response.Redirect("/IdealMarketTest1/Admin/ViewEmployeesInfo.aspx", false);
            


            }

            catch (Exception ex)
            {


            }

        }

        public void populateBranchNameDDL()
        {

            SqlConnection conn;

            conn = new
               SqlConnection("Data Source=MARAQA\\SQLEXPRESS;Initial Catalog=MarketTest1;Integrated Security=True;Pooling=False");

            SqlDataAdapter myadapter = new SqlDataAdapter("select BranchName from Branch", conn);

            DataSet myds = new DataSet();

            myadapter.Fill(myds, "Table");


            BranchNameddl.Items.Add("All");
            foreach (DataRow dr in myds.Tables[0].Rows)
            {

                BranchNameddl.Items.Add(dr["BranchName"].ToString());
            }


            conn.Close();


        }


    }

     

     

    Please Help Me I spend 3 days Searching for This Error but no Solution

     

    Thanks in advance 

     
     

     

     

     

  • Re: Please Help Me For This Bug

    05-09-2008, 9:42 AM

    You have declared your GridView as "GridView1" but many of your event handlers have a different name i.1.

        protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //CreateTemplatedGridView();
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

    Above it says GridView_PageIndexChanging not GridView1. There are a few other methods like this too. Try making the naming the same and see if that helps.

    Trevor Keast

  • Re: Please Help Me For This Bug

    05-09-2008, 10:02 AM
    • Loading...
    • Raggers
    • Joined on 02-17-2006, 4:40 PM
    • Germany
    • Posts 81

     I think here is the problem bold selectbutton.ID or deleteButton.ID because if you dont mention then asp.net  takes care of then.

    Well if these buttons all have a same ID ( means in different gridview rows) and you postback for click events then which button caused the postback ?? 

    So set  selectbutton.ID=linkbtn"+e.Row.RowIndex;

    Correct for remaining buttons also and it should be fine . 

      protected  void Employeee_RowDataBound(object sender,GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink hyperlink = new HyperLink();

              //  selectButton.ID = "linkbtn";
              //  selectButton.ImageUrl = "~/images/update.gif";
              //  selectButton.Click += new ImageClickEventHandler(select_button_Click);
                
               // selectButton.
               // dr.Table.Columns[0].ToString();
                //   hyperlink.NavigateUrl = "~/Admin/AddNewItem.aspx?"+e.Row.Cells[1].Text;
                // hyperlink.Text = "Select";
              
            ImageButton updateButton = new ImageButton();
            updateButton.PostBackUrl="~/Admin/UpdateEmployee.aspx?"+e.Row.Cells[1].Text;
            updateButton.ImageUrl = "~/images/edit.gif";
            e.Row.Cells[0].Controls.Add(updateButton);

            ImageButton deleteButton = new ImageButton();
            deleteButton.ID = "deletebtn";


           // deleteButton.PostBackUrl = "~/Admin/ViewEmployeesInfo.aspx";
            deleteButton.CommandArgument = e.Row.Cells[1].Text;
            deleteButton.ImageUrl = "~/images/delete.gif";
            deleteButton.CommandName = "Delete";
            deleteButton.OnClientClick = "return confirm('Are you sure to delete the record?')";
                          
            e.Row.Cells[0].Controls.Add(deleteButton);
          
           
            }
        }

     Yes');" title="Yes - Yes">

     

    If this solves your problem please mark as answer
Page 1 of 1 (3 items)