ASP.NET - How to get RowID from dynamic table in ASP.NET (C#)?

Asked By Sivanesh Vanmeeganathan on 20-Jun-12 01:04 AM
Earn up to 10 extra points for answering this tough question.
Here i am using like this,

Panel1.Controls.Clear();
      Table tbl = new Table();
      Panel1.Controls.Add(tbl);
      int count = 0;

      connection1.Open();
      SqlCommand cmd11 = new SqlCommand("select * from product_config where submenu='59'", connection1);
      SqlDataReader sdr11 = cmd11.ExecuteReader();
      while (sdr11.Read())
      {
        TableRow tr1 = new TableRow();
        int ii = sdr11.FieldCount;
        for (int li = 0; li < ii; li++)
        {
          if (sdr11[li].ToString() != "")
          {
            TableCell tc = new TableCell();
            chkBox = new CheckBox();
            chkBox.CheckedChanged += new EventHandler(chkBox_CheckedChanged);
            chkBox.AutoPostBack = true;
            chkBox.ID = "chk" + sdr11[li].ToString() + li + count;
            chkBox.Text = sdr11[li].ToString();
            tc.Controls.Add(chkBox);
            tr1.Cells.Add(tc);
          }
          else
          {
            TableCell tc = new TableCell();
            Label txtBox = new Label();
            tc.Controls.Add(txtBox);
            tr1.Cells.Add(tc);
          }
        }
        tbl.Rows.Add(tr1);
        count++;
      }
      sdr11.Close();
      connection1.Close();

But i cannot find checkbox checked rowid then only i get that rows checkbox text details

Kindly give me solution for get RowID
[)ia6l0 iii replied to Sivanesh Vanmeeganathan on 20-Jun-12 09:43 PM
I believe you have one checkbox per TableRow. So, your checkbox in dynamic row, should be created with an ID like this,
chkBox.ID = string.Format("chk{0}", count); 

If you want some other ID, then let us know how your code determines a different ID for the checkbox. 

Hope this helps.




help
Data Source = " + Server.MapPath("~ / ") + "db1.mdb"; / / specify the connection conn.ConnectionString = cstring; conn.Open(); / / create sqlcommand OleDbCommand cmd = conn.CreateCommand(); cmd.CommandText = "select sphoto , itemname from itemsale where itemsubcat = " + "'" + sb + "'"; OleDbDataReader dr = cmd.ExecuteReader(); Table tc = new Table(); while (dr.Read()) { / / for (int rno = 0; rno < = 2; rno TableRow tr = new TableRow(); for (int cno = 0; cno < = 2; cno++) { TableCell td = new TableCell(); Label lb = new Label variable like this; This code will add 3 cells in one row. . . . . int cnt = 0; TableRow tr; while (dr.Read()) { if(cnt = = 0) tr = new TableRow(); TableCell td = new TableCell(); Label lb = new Label(); lb.Text = "<img src = " + dr.GetString(0 Add(tc); i got error like in red line Use of unassigned local variable 'tr' TableRow tr; while (dr.Read()) { if(cnt = = 0) tr = new TableRow(); TableCell td = new TableCell(); Label lb = new Label(); lb.Text = "<img src = " + dr.GetString(0
TextBox) { if (!String.IsNullOrEmpty(c.Text)) { sql + = ", " + c.Text.Trim() + " nvarchar(MAX) NULL"; count++; } } } sql + = ")"; SqlCommand cmd = new SqlCommand(sql, con); con.Open(); cmd.ExecuteNonQuery(); } Regards, protected void Button1_Click( object sender, EventArgs e) { string ConnectionString" ; SqlConnection sqlcon = new SqlConnection (conn); sqlcon.Open(); string query = "Write your create table query" ; SqlCommand comm = new SqlCommand (query, sqlcon); comm.ExecuteNonQuery(); } In C# use a string builder to concatenate the query and columnTypes[i]); query.Append( ", " ); } if (columnNames.Length > 1) { query.Length - = 2; } / / Remove trailing ", " query.Append( ")" ); SqlCommand sqlQuery = new SqlCommand(query.ToString(), sqlConn); SqlDataReader reader = sqlQuery.ExecuteReader(); protected void Button1_Click(object sender, EventArgs e) { int rows = Convert.ToInt16(TextBox1.Text); int cols Convert.ToInt16(TextBox2.Text); for (int j = 0; j < rows; j++) { TableRow r = new TableRow(); for (int i = 0; i < cols; i++) { TableCell c = new TableCell(); c
UpdatePanel ID = "UpdatePanel1" runat = "server"> <ContentTemplate> <asp:Table runat = "server" Height = "100%" Width = "100%"> <asp:TableRow> <asp:TableCell> <asp:TextBox ID = "TextBox1" runat = "server"> < / asp:TextBox> < / asp:TableCell> < / asp:TableRow> <asp:TableRow> <asp:TableCell> <asp:Button ID = "Button4" runat = "server" Text = "Update" OnClick = "Button4_Click" / > < / asp:TableCell> < / asp TableRow> < / asp:Table> < / ContentTemplate> < / asp:UpdatePanel> <asp:Button ID = "Button3" runat = "server" Text = "Close" / > < / asp:Panel Session["vasid"] ! = null) { vasUserId = Convert.ToInt32(Session["vasid"]); if (!IsPostBack) { FillEvents(); } } } private void FillEvents() { try { SqlCommand cmd = new SqlCommand("spGetEventsByVASID", cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@VASUserId", Convert.ToInt32(Session["vasid"])); cn.Open(); SqlDataReader drEvents = cmd.ExecuteReader(); if (drEvents.HasRows) { ddlEvents.DataSource = drEvents; ddlEvents.DataTextField = "EventNAme"; ddlEvents.DataValueField = "TXTeVENTID"; ddlEvents.DataBind(); } drEvents String.Empty; return strReminderFrequency; } protected void btnSaveEvent_Click(object sender, EventArgs e) { try { int GetResult = 0; SqlCommand cmd = new SqlCommand("spAppointment_Insert", cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@EventNAme", txtEventName.Text
100%"> < / asp:Table> - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConnectionString); SqlCommand com = new SqlCommand(SelectProcedure, con); com.CommandType = CommandType.StoredProcedure; con.Open(); SqlDataReader reader = com.ExecuteReader(); int x = 0; if (reader.HasRows) { TableRow row = new TableRow(); PhotoTable.Rows.Add(row); int n = 0; while (reader.Read()) { if ((n > = offset) && (x < (int image smaller and smaller the last one is about 2 pixels tall? if (reader.HasRows) { TableRow row = new TableRow(); PhotoTable.Rows.Add(row); int n = 0; while (reader.Read()) { if ((n > = offset) && (x < Cols
SqlConnection con = new SqlConnection ( "Your db connection string" ); con.Open(); string query = "select * from yourTable" ; SqlCommand cmd = new SqlCommand (query, con); SqlDataReader dr; dr = cmd.ExecuteReader(); if (dr.Read()) { checkBox1.Checked = dr.GetBoolean(0); checkBox2.Checked = dr.GetBoolean(1); checkBox3.Checked first fill dataset ds with database value here for ( int i = 0; i < 52; i++ ) { TableRow tr = new TableRow(); / / Create column 1 TableCell td1 = new TableCell(); / / Create a checkbox control dynamically Checkbox _chk = new first fill dataset ds with database value here for ( int i = 0; i < 52; i++ ) { TableRow tr = new TableRow(); / / Create column 1 TableCell td1 = new TableCell(); / / Create a checkbox control dynamically Checkbox _chk = new Dim conn As New SqlConnection("Write Here Your Connection String") Try Dim cmd As New SqlCommand Dim dr As SqlDataReader conn.Open() cmd.Connection = conn cmd.CommandText = "Select chkField From CHK_Table
EventArgs e) { / / string constr = ConfigurationManager.ConnectionStrings["ConStr"].ToString(); / / SqlConnection con = new SqlConnection(constr); / / con.Open(); / / SqlCommand cmd = new SqlCommand("Sp_SearchUsers", con); / / cmd.CommandType = CommandType.StoredProcedure; / / cmd.Parameters.AddWithValue("@SearchText", txtSearch.Text.Trim()); / / SqlDataReader reader = cmd.ExecuteReader(); / / SqlDataSource1 = reader; / / con.Close(); bindgrid(); } public void bindgrid() { string constr = ConfigurationManager .ConnectionStrings[ "ConStr" ].ToString(); SqlConnection con = new SqlConnection (constr); con.Open(); SqlCommand cmd = new SqlCommand ( "Sp_SearchUsers" , con); cmd.CommandType = CommandType .StoredProcedure; cmd.Parameters.AddWithValue( "@SearchText" , txtSearch.Text.Trim()); SqlDataReader reader = cmd.ExecuteReader(); GridEmp.DataSource = reader; GridEmp.DataBind(); con.Close(); } protected void GridEmp_RowEditing( object sender, GridViewEditEventArgs e) { GridEmp string dept) / / { / / string constr = ConfigurationManager.ConnectionStrings["ConStr"].ToString(); / / SqlConnection con = new SqlConnection(constr); / / con.Open(); / / SqlCommand cmd = new SqlCommand("Usp_UpdateEmployee", con); / / cmd.CommandType = CommandType.StoredProcedure; / / cmd.Parameters.AddWithValue("@empid", empid); / / cmd.Parameters.AddWithValue("@name
summary> / / / Summary description for WebForm2. / / / < / summary> public class WebForm2 : System.Web.UI.Page { #region / / variaveis TableRow tableHeading = new TableRow(); / / variaveis de acesso á base de dados string sql; public SqlConnection bdconn; public SqlCommand bdcomm; public SqlDataReader bdreader; #endregion protected System.Web.UI.WebControls.Button Button1; protected System.Web o de Usage"; Descricao.HorizontalAlign = HorizontalAlign.Left; tableHeading.Cells.Add(Descricao); DisplayTable.Rows.Add(tableHeading); TableRow TableRow = new TableRow(); TableCell UsagDescri = new TableCell(); UsagDescri.Text = "teste"; TableRow.Cells.Add(UsagDescri); DisplayTable.Rows.Add(TableRow); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { / / / / CODEGEN: This call
SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { Bindgrid(); } void Bindgrid() { SqlCommand myComm = new SqlCommand("select u.Id, u.FromDate, p.Purpose, s.SubPurpose, u.Unit, u.MaxLoan, u.MinLoan u.ProductId = lp.Id and u.SubPurposeId = s.ID", myConn); myConn.Open(); SqlDataReader myReader = myComm.ExecuteReader(); Grid1.ClientSideEvents.OnBeforeClientDelete = "OnBeforeDelete"; Grid1.InsertCommand + = new Obout.Grid.Grid.EventHandler(InsertRecord); Grid1.DeleteCommand + = new DataSource = myReader; Grid1.DataBind(); myConn.Close(); } void DeleteRecord(object sender, GridRecordEventArgs e) { Bindgrid(); myConn.Open(); SqlCommand myComm = new SqlCommand("DELETE FROM Loan_UnitCost WHERE Id = @Id", myConn); myComm.Parameters.Add("@Id", SqlDbType.Int).Value = e Record["Id"]; myComm.ExecuteNonQuery(); myConn.Close(); Bindgrid(); } void UpdateRecord(object sender, GridRecordEventArgs e) { myConn.Open(); SqlCommand myComm1 = new SqlCommand("Select Id from Loan_Purpose where Purpose = '" + e.Record["Purpose"].ToString() + "'", myConn); object obj = myComm1.ExecuteScalar(); int i = Convert.ToInt32(obj); SqlCommand myComm2 = new SqlCommand("Select Id from Loan_SubPurpose where SubPurpose = '" + e.Record["SubPurpose"].ToString() + "'", myConn); object
SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { Bindgrid(); } void Bindgrid() { SqlCommand myComm = new SqlCommand("select u.Id, u.FromDate, p.Purpose, s.SubPurpose, u.Unit, u.MaxLoan, u.MinLoan u.ProductId = lp.Id and u.SubPurposeId = s.ID", myConn); myConn.Open(); SqlDataReader myReader = myComm.ExecuteReader(); Grid1.ClientSideEvents.OnBeforeClientDelete = "OnBeforeDelete"; Grid1.InsertCommand + = new Obout.Grid.Grid.EventHandler(InsertRecord); Grid1.DeleteCommand + = new DataSource = myReader; Grid1.DataBind(); myConn.Close(); } void DeleteRecord(object sender, GridRecordEventArgs e) { Bindgrid(); myConn.Open(); SqlCommand myComm = new SqlCommand("DELETE FROM Loan_UnitCost WHERE Id = @Id", myConn); myComm.Parameters.Add("@Id", SqlDbType.Int).Value = e Record["Id"]; myComm.ExecuteNonQuery(); myConn.Close(); Bindgrid(); } void UpdateRecord(object sender, GridRecordEventArgs e) { myConn.Open(); SqlCommand myComm1 = new SqlCommand("Select Id from Loan_Purpose where Purpose = '" + e.Record["Purpose"].ToString() + "'", myConn); object obj = myComm1.ExecuteScalar(); int i = Convert.ToInt32(obj); SqlCommand myComm2 = new SqlCommand("Select Id from Loan_SubPurpose where SubPurpose = '" + e.Record["SubPurpose"].ToString() + "'", myConn); object