I have dynamically created a dropdown and everything is fine once the page loads. There is a button in each row of the gridview next to the dropdown. This button updates the table for the selected item. I’m getting my key, but I"m getting object reference not set to an instance. Is that because the id of the dropdown control is burried in ct100 stuff?
Dim ddluid As String = CType(FindControl("Ddlos"), DropDownList).SelectedItem.Value
This here I have swiped from the source of the page:
<select name="ctl00$ContentPlaceHolder2$GridView1$ctl02$Ddlos" id="ctl00_ContentPlaceHolder2_GridView1_ctl02_Ddlos">
<option value="0">-Select-</option>
<option value="503841a3-c615-4e4d-8cf5-20fbddbf7a7f">John Doe</option>
<option value="9b38e9cd-f16f-4fdd-a82e-501c866f9e45">Sam Beacon</option>
<option value="fe6158c5-a549-443f-b5ff-c011937db1d7">John Doey</option>
<option value="295e9f85-6ea6-46ec-9c00-c38d64023517">Scot King</option>
Protected Sub btnsaveinfo(sender As Object, e As EventArgs)
Dim key As String = ""
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
key = GridView1.DataKeys(row.RowIndex).Value.ToString()
Dim ddluid As String = CType(FindControl("Ddlos"), DropDownList).SelectedItem.Value
'Dim idvalue As String = ddluid.SelectedValue
'Dim louid As Guid =
Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("sqlConnectionString").ConnectionString)
Dim cmdupdate As New SqlCommand("update Profiles SET [loanrepid] = @loanrepid WHERE ApplicantID=@ApplicantID", cn)
cmdupdate.Parameters.AddWithValue("@loanrepid", "1")
cmdupdate.Parameters.AddWithValue("@ApplicantID", key)
cmdupdate.CommandType = System.Data.CommandType.Text
cmdupdate.Connection = cn
cn.Open()
cmdupdate.ExecuteNonQuery()
cn.Close()
End If
Next
lblresult.Text = " Details Updated Successfully"
lblresult.Visible = True
End Sub
<asp:TemplateField HeaderText="Loan Officer" SortExpression="Loan Officer" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="Ddlos" runat="server" Visible="false"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
2
Answers
thank you for your advice on the problem. Yes, I have it as one button now. I had a couple things there that were causing it to not work. I figure just one button to resave the selections on the page.
A a few things.
You could have the user make all changes, and the below the grid, one save button.
however, since you have a button on each row, to save the row back to database?
Then no need exists to loop all rows in the one save button row????
However, what happens if the user changes several rows – forgets to hit save, and then does hit save? In other words, the user going to be somewhat confused here?
So, it not clear that clicking on the save button should only save the current row?
If that is the goal, then we could have say this setup:
And code to load would be:
and we now have this:
Ok, so the save button – to save the one row?
this works:
So, as you can see, no real need to loop and save ALL rows, is there?
I would think that if you want ONE save button?
Then move the save button OUT of the grid, and then save have this:
And now this:
And the one save button (which I think after saving should say navagate to some other page – since we are done). That code to now save all rows, would be:
So, again the code is rather simular for saving "all" rows, or just the one row.
However, we could also drop in a button beside "save" called "un-do" or "cancel"
So, the above shows how to get the row click – for ONE row save, and the 2nd code shows how do to this for all rows.