0

I have a GridView which has the property:

OnRowUpdating="GridViewRowUpdateEventHandler"

Within the GridView, I have the following control:

<asp:TemplateField HeaderText="Progress" SortExpression="progress">
    <ItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
            <asp:ListItem Value="0">Incomplete</asp:ListItem>
            <asp:ListItem Value="1">Complete</asp:ListItem>
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField> 

The GridViewRowUpdateEventHandler looks like this:

protected void GridViewRowUpdateEventHandler(object sender, GridViewUpdateEventArgs e)
{
    SqlConnection connection;
    SqlCommand command;

    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

    DropDownList ddlPriority = (DropDownList)row.FindControl("DropDownList1");

    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
    {
        using (command = new SqlCommand(@"update table1 set priority = @priority where id = @id", connection))
        {
            command.Parameters.Add("@priority", SqlDbType.Int, 1).Value = ddlPriority.SelectedValue;
            command.Parameters.Add("@id", SqlDbType.Int, 1).Value = row.RowIndex;

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }
    }

    GridView1.DataBind();
}

I am getting no error messages at all, and the relevant row in the database is not being updated. Anyone know why?

Could it be that I am looking at the wrong cell Cells[5]? Or maybe because I have nothing in page_load? Or maybe something else?

2
  • If you were looking at the wrong cell, it would throw you an exception for referring to the dropdown Commented Aug 31, 2012 at 15:14
  • Updated update code based on answers, but I am still getting the same result, i.e. no errors, and no database update. I think I will have to sort out the VS2008 debugging on my PC first. Commented Aug 31, 2012 at 15:42

5 Answers 5

1

Since the DropDiownList is in a TemplateField and it's NamingContainer is the GridViewRow, you should use row.FindControl to get the reference:

DropDownList ddlPriority = (DropDownList)row.FindControl("DropDownList1");

instead of

DropDownList ddlPriority = (DropDownList)row.Cells[5].FindControl("DropDownList1");

But that is not the core of your problem since row.Cells[5].FindControl might work also when it is in the 6th cell. Otherwise you would get a NullreferenceException.

I assume that you are also binding the GridView on postbacks, you should check the IsPostBack property:

protected void Page_Load()
{
    if (!IsPostBack)
    {
        BindGrid();
    }
}

Apart from that:

command.Parameters.Add("@id", SqlDbType.Int, 1).Value = row;

Does not work because row is the GridViewRow. You should also DataBind your GridView at the end, otherwise the changes won't be reflected.

Sign up to request clarification or add additional context in comments.

3 Comments

Updated update code based on answers, but I am still getting the same result, i.e. no errors, and no database update. I think I will have to sort out the VS2008 debugging on my PC first. Also, I am not binding on postback, I don't have a BindGrid() at all...
@oshirowanen: So are you using a SqlDataSource or any other declarative datasource control? Yes, use the debugger to see what happens.
Yes, I'm using the <asp:SqlDataSource ... ></asp:SqlDataSource> control. As soon as I have the debugging sorted, I'll post back here.
1

You add DataBind in the end of your GridViewRowUpdateEventHandler

GridView.DataBind();

In order to find control

var ddlPriority = (DropDownList)row.FindControl("DropDownList1");

2 Comments

Updated update code based on answers, but I am still getting the same result, i.e. no errors, and no database update. I think I will have to sort out the VS2008 debugging on my PC first.
Ok i'am here if you need help when you debug
1

I would assume that you have forgotten to rebind your gridView data

gridview1.DataSource = YOUR_DATASOURCE;
gridview1.DataBind();

Furthermore you should access your dropDownList by

DropDownList ddlPriority = (DropDownList) row.FindControl("DropDownList1");

EDIT

Found another error

command.Parameters.Add("@id", SqlDbType.Int, 1).Value = row; // wrong! primaryKey value needed!

this won't work as row is of type GridViewRow. You should assign your primaryKey value I assume!

No matter what you decide to do first I would add some Debug.WriteLine(..) statements to see what values will be sent to the SQL Server.

3 Comments

For some reason why I try to debug using VS2008, it doesn't work. I guess it's not configured properly. Any other way of checking the values?
next to your start button of your project should be "Debug" and within your web.config something like <compilation debug="true" targetFramework="XX"> give it try. Debugging is crucial to all kind of applications!
Updated update code based on answers, but I am still getting the same result, i.e. no errors, and no database update. I think I will have to sort out the VS2008 debugging on my PC first.
1

I think the issue is that you are using the row's rowIndex as the ID. So your sql command at the end would be something like this

update table1 set priority = 'blah' where id = 0 //(where its the first row and so on)

So it is a perfectly good query, which will run without errors or exceptions but there is no ID = 0. You are binding it to the wrong ID. What you need to bind it to, is the ID from the table.

You can double check this by running SQL profiler and you should be able to locate the query that is being sent to the database.

Comments

0

You need to update the EditIndex of GridView

as

GridView.EditIndex = -1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.