Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on Asp .Net web forms right now. I am using tables to show my data from the database. I have created actions for Insert, Edit, Delete but the Insert is only working. I want to do edit and delete but i am having hard time getting the id of the selected row in order to edit or delete the row from the table. This is what i was doing.

What I have tried:

public void DisplayRecord()
        {

            using (SqlConnection mySqlConnection = new SqlConnection(connectionString))
            {
                using (SqlCommand mySqlCommand = new SqlCommand("ReadTransactions", mySqlConnection))
                {
                    try
                    {
                        mySqlCommand.CommandType = CommandType.StoredProcedure;
                        //mySqlCommand.Connection = mySqlConnection;
                        mySqlConnection.Open();
                        using (SqlDataReader QueryReader = mySqlCommand.ExecuteReader())
                        {
                            if (QueryReader.HasRows)
                            {
                                while (QueryReader.Read())
                                {
                                    string id = QueryReader.GetString(0);
                                    string date = QueryReader.GetDateTime(1).ToString();
                                    string amount = QueryReader.GetInt32(2).ToString();
                                    string quantity = QueryReader.GetInt32(3).ToString();
                                    string edit = "";
                                    //table.Append("<table>");                                                                     
                                    data += "<tr><td>" + id + "</td><td>" + date + "</td><td>" + amount + "</td><td>" + quantity + "</td><td>" + "<button UseSubmitBehavior='False' type='button' runat='server' OnClick='edit_click'>Edit</button>" + "</td></tr>";
                                }
                            }
                            else
                            {
                                table.Append("<tr>No Record Found</tr>");
                            }
                        }
                    }
                    catch (SqlException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        mySqlConnection.Close();
                    }
                    dataHolder.Text = data;
                }
            }
        }



        protected void edit_click(object sender, EventArgs e) {
            Console.WriteLine("Click Me");
            Console.Read();
            paymentBox.Text = "";
            quantityBox.Text = "dasf";
        }
Posted
Comments
F-ES Sitecore 18-Apr-18 10:28am    
Go through tutorials on editable gridviews, you can't create server controls in that way, and you can't issue console commands like Console.Read either, your code is running on the server so someone will need to provide console input on the server.
Subit Timalsina 18-Apr-18 12:19pm    
I have already done with gridview but i wanted to try it out with html tables isn't it possible through plain html table?
an0ther1 18-Apr-18 17:36pm    
Everything is possible, you have time, money & good... Pick 2.
EG: I want it to be fast & cheap - it will not look good
I want it fast and good - it will not be cheap

If you are creating a table on the server and inserting it into you HTML you can get the data from it in the code behind but it is messy & not really what would be considered best practice. as F-ES Sitecore has suggested, use an editable GridView - this is the correct solution.
Concatenating strings and passing them to the page means a lot of hard-coding & changes in the future will be unreasonable.
If you insist on making a table then use an ASP table & add columns and rows to it - refer; MSDN Table Class
F-ES Sitecore 19-Apr-18 4:20am    
It's possible but you have to use "raw" html coding, you can't create server-controls with click events like that. You'd need to either create dynamic server controls, or if you want to manipulate the html use standard html controls. So you would create html like

<input type="submit" name="btnEdit" value="Edit" />

To run server code when the button is clicked, in the page load event check

if (Request.Form["btnEdit"] == "Edit")

and if so run the code you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900