Answered by:
dropdownlist in gridview edit mode won't save value

Question
-
User-828040185 posted
I have a column in my gridview that is empty by default (string). Then when user goes to edit he needs to update that row and in that column he must write Accepted or Denied. To remove possibiliti of error, instead of text box I inserted a dropdown list with two item I entered manually. After i click update dropdown list appears and it has two items in it, but after I press save no vakue is saved into database, it says null :(
This is my code:
<asp:GridView ID="gvNeobrađene" HorizontalAlign="Center" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="sdsNeobradene" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowUpdating="gvNeobrađene_RowUpdating"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:BoundField DataField="Ime" HeaderText="Ime" SortExpression="Ime" /> <asp:BoundField DataField="Prezime" HeaderText="Prezime" SortExpression="Prezime" /> <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /> <asp:BoundField DataField="Telefon" HeaderText="Telefon" SortExpression="Telefon" /> <asp:BoundField DataField="Napomena" HeaderText="Napomena" SortExpression="Napomena" /> <asp:BoundField DataField="SeminarID" HeaderText="SeminarID" SortExpression="SeminarID" /> <asp:BoundField DataField="TerminID" HeaderText="TerminID" SortExpression="TerminID" /> <asp:CheckBoxField DataField="PotvrdaP" HeaderText="PotvrdaP" SortExpression="PotvrdaP" ReadOnly="false"/> <asp:TemplateField HeaderText="StatusP" SortExpression="StatusP"> <EditItemTemplate> <asp:DropDownList ID="StatusP" runat="server"> <asp:ListItem Value="">Odaberi status</asp:ListItem> <asp:ListItem Value="Prihvaćena">Prihvaćena</asp:ListItem> <asp:ListItem Value="Odbijena">Odbijena</asp:ListItem> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("StatusP") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ButtonType="Button" CancelText="Odustani" DeleteText="Izbriši" EditText="Uredi" InsertText="Ubaci" NewText="Novo" SelectText="Odaberi" ShowEditButton="True" ShowHeader="True" UpdateText="Spremi" ShowDeleteButton="True" /> </Columns> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F5F7FB" /> <SortedAscendingHeaderStyle BackColor="#6D95E1" /> <SortedDescendingCellStyle BackColor="#E9EBEF" /> <SortedDescendingHeaderStyle BackColor="#4870BE" /> </asp:GridView> <asp:SqlDataSource ID="sdsNeobradene" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:SeminarBazaConnectionString %>" DeleteCommand="DELETE FROM [Predbiljezba] WHERE [Id] = @original_Id AND [Ime] = @original_Ime AND [Prezime] = @original_Prezime AND [Email] = @original_Email AND [Telefon] = @original_Telefon AND (([Napomena] = @original_Napomena) OR ([Napomena] IS NULL AND @original_Napomena IS NULL)) AND [SeminarID] = @original_SeminarID AND [TerminID] = @original_TerminID AND [PotvrdaP] = @original_PotvrdaP AND (([StatusP] = @original_StatusP) OR ([StatusP] IS NULL AND @original_StatusP IS NULL))" InsertCommand="INSERT INTO [Predbiljezba] ([Ime], [Prezime], [Email], [Telefon], [Napomena], [SeminarID], [TerminID], [PotvrdaP], [StatusP]) VALUES (@Ime, @Prezime, @Email, @Telefon, @Napomena, @SeminarID, @TerminID, @PotvrdaP, @StatusP)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Predbiljezba] WHERE ([PotvrdaP] = @PotvrdaP)" UpdateCommand="UPDATE [Predbiljezba] SET [Ime] = @Ime, [Prezime] = @Prezime, [Email] = @Email, [Telefon] = @Telefon, [Napomena] = @Napomena, [SeminarID] = @SeminarID, [TerminID] = @TerminID, [PotvrdaP] = @PotvrdaP, [StatusP] = @StatusP WHERE [Id] = @original_Id"> <DeleteParameters> <asp:Parameter Name="original_Id" Type="Int32" /> <asp:Parameter Name="original_Ime" Type="String" /> <asp:Parameter Name="original_Prezime" Type="String" /> <asp:Parameter Name="original_Email" Type="String" /> <asp:Parameter Name="original_Telefon" Type="String" /> <asp:Parameter Name="original_Napomena" Type="String" /> <asp:Parameter Name="original_SeminarID" Type="Int32" /> <asp:Parameter Name="original_TerminID" Type="Int32" /> <asp:Parameter Name="original_PotvrdaP" Type="Boolean" /> <asp:Parameter Name="original_StatusP" Type="String" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="Ime" Type="String" /> <asp:Parameter Name="Prezime" Type="String" /> <asp:Parameter Name="Email" Type="String" /> <asp:Parameter Name="Telefon" Type="String" /> <asp:Parameter Name="Napomena" Type="String" /> <asp:Parameter Name="SeminarID" Type="Int32" /> <asp:Parameter Name="TerminID" Type="Int32" /> <asp:Parameter Name="PotvrdaP" Type="Boolean" /> <asp:Parameter Name="StatusP" Type="String" /> </InsertParameters> <SelectParameters> <asp:Parameter DefaultValue="False" Name="PotvrdaP" Type="Boolean" /> </SelectParameters> <UpdateParameters> <asp:Parameter Name="Ime" Type="String" /> <asp:Parameter Name="Prezime" Type="String" /> <asp:Parameter Name="Email" Type="String" /> <asp:Parameter Name="Telefon" Type="String" /> <asp:Parameter Name="Napomena" Type="String" /> <asp:Parameter Name="SeminarID" Type="Int32" /> <asp:Parameter Name="TerminID" Type="Int32" /> <asp:Parameter Name="PotvrdaP" Type="Boolean" /> <asp:Parameter Name="StatusP" Type="String" /> <asp:Parameter Name="original_Id" Type="Int32" /> <asp:Parameter Name="original_Ime" Type="String" /> <asp:Parameter Name="original_Prezime" Type="String" /> <asp:Parameter Name="original_Email" Type="String" /> <asp:Parameter Name="original_Telefon" Type="String" /> <asp:Parameter Name="original_Napomena" Type="String" /> <asp:Parameter Name="original_SeminarID" Type="Int32" /> <asp:Parameter Name="original_TerminID" Type="Int32" /> <asp:Parameter Name="original_PotvrdaP" Type="Boolean" /> <asp:Parameter Name="original_StatusP" Type="String" /> </UpdateParameters> </asp:SqlDataSource>
Never done this before, so don't really know what went wrong. Am I missing a step or something - I think so. Please every advice is helpful. Thank you.
Monday, July 18, 2016 9:09 PM
Answers
-
User283571144 posted
Hi Goran_C#,
To remove possibiliti of error, instead of text box I inserted a dropdown list with two item I entered manually. After i click update dropdown list appears and it has two items in it, but after I press save no vakue is saved into database, it says null :(I suggest you could use OnRowUpdating in gridview and get all updata value in edittemplete in code-behind.
How to get updata value, you could refer to follow codes:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows[e.RowIndex]; int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]); TextBox name1 = GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox; TextBox password1 = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox; DropDownList CityId1 = GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList; string name = name1.Text; string password = password1.Text; int CityId = Convert.ToInt32(CityId1.SelectedValue); SqlDataSource1.UpdateCommand = "UPDATE [UserInfo] SET [name] = @name, [password] = @password, [CityId] = @CityId WHERE [Id] = @Id"; SqlDataSource1.UpdateParameters.Add("name",DbType.String, name); SqlDataSource1.UpdateParameters.Add("password", DbType.String, password); SqlDataSource1.UpdateParameters.Add("CityId", DbType.Int32, CityId.ToString()); SqlDataSource1.UpdateParameters.Add("Id", DbType.Int32, Id.ToString()); }
More details you could refer to follow codes:
<asp:GridView ID="GridView1" runat="server" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" DataKeyNames="Id" DataSourceID="SqlDataSource1"> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:TemplateField HeaderText="name" SortExpression="name"> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("name") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="password" SortExpression="password"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("password") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("password") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CityId" SortExpression="CityId"> <EditItemTemplate > <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Value="1" >1</asp:ListItem> <asp:ListItem Value="2" >2</asp:ListItem> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("CityId") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:UserConnectionString2 %>" SelectCommand="SELECT * FROM [UserInfo]"> </asp:SqlDataSource>
Code-Behind:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows[e.RowIndex]; int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]); TextBox name1 = GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox; TextBox password1 = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox; DropDownList CityId1 = GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList; string name = name1.Text; string password = password1.Text; int CityId = Convert.ToInt32(CityId1.SelectedValue); SqlDataSource1.UpdateCommand = "UPDATE [UserInfo] SET [name] = @name, [password] = @password, [CityId] = @CityId WHERE [Id] = @Id"; SqlDataSource1.UpdateParameters.Add("name",DbType.String, name); SqlDataSource1.UpdateParameters.Add("password", DbType.String, password); SqlDataSource1.UpdateParameters.Add("CityId", DbType.Int32, CityId.ToString()); SqlDataSource1.UpdateParameters.Add("Id", DbType.Int32, Id.ToString()); }
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 19, 2016 7:56 AM
All replies
-
User283571144 posted
Hi Goran_C#,
To remove possibiliti of error, instead of text box I inserted a dropdown list with two item I entered manually. After i click update dropdown list appears and it has two items in it, but after I press save no vakue is saved into database, it says null :(I suggest you could use OnRowUpdating in gridview and get all updata value in edittemplete in code-behind.
How to get updata value, you could refer to follow codes:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows[e.RowIndex]; int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]); TextBox name1 = GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox; TextBox password1 = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox; DropDownList CityId1 = GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList; string name = name1.Text; string password = password1.Text; int CityId = Convert.ToInt32(CityId1.SelectedValue); SqlDataSource1.UpdateCommand = "UPDATE [UserInfo] SET [name] = @name, [password] = @password, [CityId] = @CityId WHERE [Id] = @Id"; SqlDataSource1.UpdateParameters.Add("name",DbType.String, name); SqlDataSource1.UpdateParameters.Add("password", DbType.String, password); SqlDataSource1.UpdateParameters.Add("CityId", DbType.Int32, CityId.ToString()); SqlDataSource1.UpdateParameters.Add("Id", DbType.Int32, Id.ToString()); }
More details you could refer to follow codes:
<asp:GridView ID="GridView1" runat="server" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" DataKeyNames="Id" DataSourceID="SqlDataSource1"> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:TemplateField HeaderText="name" SortExpression="name"> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("name") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="password" SortExpression="password"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("password") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("password") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CityId" SortExpression="CityId"> <EditItemTemplate > <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Value="1" >1</asp:ListItem> <asp:ListItem Value="2" >2</asp:ListItem> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("CityId") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:UserConnectionString2 %>" SelectCommand="SELECT * FROM [UserInfo]"> </asp:SqlDataSource>
Code-Behind:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows[e.RowIndex]; int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]); TextBox name1 = GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox; TextBox password1 = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox; DropDownList CityId1 = GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList; string name = name1.Text; string password = password1.Text; int CityId = Convert.ToInt32(CityId1.SelectedValue); SqlDataSource1.UpdateCommand = "UPDATE [UserInfo] SET [name] = @name, [password] = @password, [CityId] = @CityId WHERE [Id] = @Id"; SqlDataSource1.UpdateParameters.Add("name",DbType.String, name); SqlDataSource1.UpdateParameters.Add("password", DbType.String, password); SqlDataSource1.UpdateParameters.Add("CityId", DbType.Int32, CityId.ToString()); SqlDataSource1.UpdateParameters.Add("Id", DbType.Int32, Id.ToString()); }
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 19, 2016 7:56 AM -
User-828040185 posted
Thank you very much for your reply I will try this when I get home after work :)
Tuesday, July 19, 2016 10:32 AM