Error updating data from a GridView with DropDown

0

I have a GridView in which I bring information from the database, and with it I also fill DropDownList but when I want to edit the records this fails and tells me that it could not be converted from DropDownList a String or Int .

This is the code with which I tried to do the update :

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string myConnectionString = @"C:\Users\gutiece\Desktop\database\" + "Database1.accdb";

    using (OleDbConnection connection = new OleDbConnection())
    {
        using (OleDbCommand command = new OleDbCommand())
        {
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
            command.Connection = connection;
            connection.Open();

            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            DropDownList tool = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool");
            DropDownList area = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool");
            DropDownList pool = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool");
            DropDownList team = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool");
            DropDownList station = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool");
            DropDownList operation = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool");
            DropDownList torque = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool");
            DropDownList tl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool");

            command.CommandText = "UPDATE area SET tool_id = '" + tool + "', area = '" + area + "', pool = '" + pool + "', team = " + team + ", station = '" + station + "', operation = '" + operation + "', operation_torque = " + torque + ", tl_name = '" + tl + "' WHERE id =  = " + id + "";

            command.Parameters.Add("@id", OleDbType.Integer).Value = id;
            command.Parameters.Add("@TOOL_ID", OleDbType.VarChar).Value = tool;
            command.Parameters.Add("@AREA", OleDbType.VarChar).Value = area;
            command.Parameters.Add("@POOL", OleDbType.VarChar).Value = pool;
            command.Parameters.Add("@TEAM", OleDbType.Integer).Value = team;
            command.Parameters.Add("@STATION", OleDbType.VarChar).Value = station;
            command.Parameters.Add("@OPERATION", OleDbType.VarChar).Value = operation;
            command.Parameters.Add("@OPERATION_TORQUE", OleDbType.Integer).Value = torque;
            command.Parameters.Add("@TL_NAME", OleDbType.VarChar).Value = tl;

            command.ExecuteNonQuery();

            GridView1.DataBind();
            GridView1.EditIndex = -1;
            Response.Redirect("asignacion.aspx");
            connection.Close();
        }
    }
}
  

Failed to convert parameter value from to DropDownList to a String.

I made a BreakPoint and in each value, nothing comes in only this appears as a value of the variable {System.Web.UI.WebControls.DropDownList}

    
asked by Cesar Gutierrez Davalos 24.05.2017 в 18:49
source

2 answers

0

The problem lies in this line:

command.CommandText = "UPDATE area SET tool_id = '" + tool + "', area = '" + area + "', pool = '" + pool + "', team = " + team + ", station = '" + station + "', operation = '" + operation + "', operation_torque = " + torque + ", tl_name = '" + tl + "' WHERE id =  = " + id + "";

Taking as an example the variable tool you are concatenating it as if it were a string when it is of type DropDownList .

The solution is to arm the sentence with parameters:

command.CommandText = "UPDATE area SET tool_id = @TOOL_ID, area.....";

So that later, you indicate the values that you must assign to those parameters (it can be with SelectedValue or SelectedIndex depending on the one that you want to save in the database):

command.Parameters.Add("@TOOL_ID", OleDbType.VarChar).Value = tool.SelectedIndex;

And so with the rest of the parameters.

    
answered by 24.05.2017 в 19:02
0

To each of the DropDownList you must access the property SelectedValue .

Example:

string tool = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool")).SelectedValue;
string area = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool")).SelectedValue;
string pool = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool")).SelectedValue;
string team = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool")).SelectedValue;
string station = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool")).SelectedValue;
string operation = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool")).SelectedValue;
string torque = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool")).SelectedValue;
string tl = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("gv_tool")).SelectedValue;
    
answered by 24.05.2017 в 18:59