简体   繁体   中英

Update sql Data from ASP:Gridview Textbox

I have an ASPX application that displays 3 ASP:gridview objects. I've selected certain columns from a SQLite table to show in each gridview based on the value in those columns. The problem I'm having is with one gridview. This gridview pulls 6 columns from the table and displays them along with 2 ButtonFields and 1 Templatefield with a texbox control in it. One of the buttons is supposed to insert the text from the textbox (entered by the user) into the table. The app then refreshes the gridview and the inserted data should show in the column.

I'm using the following to insert data into the table:

    protected void gvApproved_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int rowIndex = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = gvApproved.Rows[rowIndex];
        string name = row.Cells[0].Text;
        string column = "";
        string value = "";
        switch (e.CommandName)
        {
            case "Close":
                column = "closed";
                value = "yes";
                break;
            case "Score":
                column = "score";
                value = row.Cells[8].Text;
                break;
        }
        SQL_Helper.Update_Row(rowIndex, column, value, name);
        Refresh_All_Grids();
    }

The update row command, in case anyone is curious:

    public static void Update_Row(int rowIndex, string updateField, string updateString, string updateName)
    {
        string query = "UPDATE applications SET '" + updateField + "' = '" + updateString + "' WHERE name = '" + updateName + "';";
        Query_Table(query);
    }

When I set the line value = row.Cells[8].Text; to value = row.Cells[8].ToString() , it will insert 'System.Web.UI.WebControls.DataControlFieldCell' into the table, so I know it's pulling from the right column, but when I leave it as written above, it inserts an empty string. Is it not possible to insert from a gridview Textbox? If it is, what do I need to change to make it work?

I have my previously posted answer below, but then with more looking into it I realized you're working with GridViewRow, not DataGridViewRow. You should update the question to not include anything about DataGridView. GridViewRow.Cells[8] doesn't have Value like I mentioned in my below answer.

I was going to just comment to see if this small suggestion fixed it, but I don't have 50 rep yet. You have to use Value instead of just the Cell itself.

Does this work?

 value = row.Cells[8].Value.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM