GridView Update Problemhttp://forums.asp.net/t/1776429.aspx/1?GridView+Update+ProblemTue, 13 Mar 2012 08:15:36 -040017764294862487http://forums.asp.net/p/1776429/4862487.aspx/1?GridView+Update+ProblemGridView Update Problem <p>Hi all,</p> <p>I am using MS Visual Studio 2010, VB Language.</p> <p>I am trying to add, Update, and delete records&nbsp; using GridView Control. Inside the GridView I have a DropDownList control (MaintenanceTermID ) as a session.&nbsp; I can add and delete the records successfully, but the update is not working.</p> <p>Once I edit the data and click the Update button, it deletes the row from the GridView! In the database they are available but the MaintenanceTermID (session) becomes NULL!!!</p> <p>I have no idea why this is happening !</p> <p>I need your help please as soon as possible</p> <p>Source Code:</p> <pre class="prettyprint">&lt;asp:SqlDataSource ID=&quot;SqlDataSourceMaintenance&quot; runat=&quot;server&quot; ConnectionString=&quot;&lt;%$ ConnectionStrings:maintenanceDBConnectionString %&gt;&quot; SelectCommand=&quot;SELECT tbl_MaintenanceSubTemp.Quantity, tbl_MaintenanceSubTemp.Total, tbl_MaintenanceSubTemp.MaintenanceSubTempID, tbl_MaintenanceTerms.MaintenanceTermNo FROM tbl_MaintenanceSubTemp INNER JOIN tbl_MaintenanceTerms ON tbl_MaintenanceSubTemp.MaintenanceTermsID = tbl_MaintenanceTerms.MaintenanceTermsID&quot; InsertCommand=&quot;Insert into tbl_MaintenanceSubTemp (MaintenanceTermsID, Quantity, Total) values (@MaintenanceTermsID, @Quantity, @Total)&quot; UpdateCommand=&quot;Update tbl_MaintenanceSubTemp SET Quantity=@Quantity, MaintenanceTermsID=@MaintenanceTermsID, Total=@Total where (MaintenanceSubTempID=@MaintenanceSubTempID)&quot; DeleteCommand=&quot;Delete from tbl_MaintenanceSubTemp where (MaintenanceSubTempID=@MaintenanceSubTempID)&quot; oninserting=&quot;Sqldatasource1_Inserting&quot; OnInserted=&quot;Sqldatasource1_Inserted&quot;&gt; &lt;DeleteParameters&gt; &lt;asp:Parameter Name=&quot;MaintenanceSubTempID&quot; /&gt; &lt;/DeleteParameters&gt; &lt;InsertParameters&gt; &lt;asp:Parameter Name=&quot;MaintenanceTermsID&quot; /&gt; &lt;asp:Parameter Name=&quot;Quantity&quot; /&gt; &lt;asp:Parameter Name=&quot;Total&quot; /&gt; &lt;/InsertParameters&gt; &lt;UpdateParameters&gt; &lt;asp:Parameter Name=&quot;MaintenanceTermsID&quot; /&gt; &lt;asp:Parameter Name=&quot;Quantity&quot; /&gt; &lt;asp:Parameter Name=&quot;Total&quot; /&gt; &lt;asp:Parameter Name=&quot;MaintenanceSubTempID&quot; /&gt; &lt;/UpdateParameters&gt; &lt;/asp:SqlDataSource&gt;</pre> <pre class="prettyprint">&lt;asp:GridView ID="GridViewMaintenance" runat="server" dir="rtl" AutoGenerateColumns="False" DataKeyNames="MaintenanceSubTempID" DataSourceID="SqlDataSourceMaintenance" ShowFooter="True" RowStyle-BorderColor="beige" RowStyle-BorderWidth="1" BorderWidth="2px" BorderColor="Silver" Font-Names="Tahoma" Font-Size="Small"&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText="" SortExpression="MaintenanceSubTempID"&gt; &lt;ItemTemplate&gt; &lt;%# Eval("MaintenanceSubTempID")%&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=" " SortExpression="MaintenanceTermsID"&gt; &lt;ItemTemplate&gt; &lt;%# Eval("MaintenanceTermNo")%&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:DropDownList ID="EditTermNo" runat="server" AutoPostBack="True" DataSourceID="SqlDataSourcetermNo" DataTextField="MaintenanceTermNo" DataValueField="MaintenanceTermsID" AppendDataBoundItems="True" onselectedindexchanged="EditTermNo_SelectedIndexChanged"&gt; &lt;asp:ListItem Selected="True" Value="0"&gt;choose&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;a href="PopupTerms.aspx" onclick="return popitup('PopupTerms.aspx')"&gt;&lt;/a&gt; &lt;/EditItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:DropDownList ID="FooterTermNo" runat="server" AutoPostBack="True" DataSourceID="SqlDataSourcetermNo" DataTextField="MaintenanceTermNo" DataValueField="MaintenanceTermsID" AppendDataBoundItems="True"&gt; &lt;asp:ListItem Selected="True" Value="0"&gt;choose&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;a href="PopupTerms.aspx" onclick="return popitup('PopupTerms.aspx')"&gt;&lt;/a&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="" SortExpression="Quantity"&gt; &lt;ItemTemplate&gt; &lt;%# Eval("Quantity")%&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID="TextBoxEditQty" runat="server" Text='&lt;%# Bind("Quantity") %&gt;'&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:TextBox ID="TextBoxInsertQty" runat="server" Text='&lt;%# Bind("Quantity") %&gt;'&gt;&lt;/asp:TextBox&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="" SortExpression="Total"&gt; &lt;ItemTemplate&gt; &lt;%# Eval("Total")%&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID="TextBoxEditTotal" runat="server" Text='&lt;%# Bind("Total") %&gt;' style="margin-right: 0px"&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:TextBox ID="TextBoxInsertTotal" runat="server" Text='&lt;%# Bind("Total") %&gt;' ReadOnly="True"&gt;&lt;/asp:TextBox&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:Button ID="Edit" runat="server" CommandName="Edit" Text="" Font-Names="Tahoma" Font-Size="Small"/&gt; &lt;asp:Button ID="Delete" runat="server" CommandName="Delete" Text="" Font-Names="Tahoma" Font-Size="Small" /&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:Button ID="ButtonMultiplyEdit" runat="server" CommandName="MultiplyEdit" Font-Names="Tahoma" Font-Size="Small" Text="x" /&gt; &lt;asp:Button ID="Update" runat="server" CommandName="Update" Text="" Font-Names="Tahoma" Font-Size="Small" /&gt; &lt;asp:Button ID="Cancel" runat="server" CommandName="Cancel" Text="" Font-Names="Tahoma" Font-Size="Small" /&gt; &lt;/EditItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:Button ID="ButtonMultiplyFooter" runat="server" Font-Size="Small" Text="x" Font-Names="Tahoma" CommandName="MultiplyFooter" /&gt; &lt;asp:Button ID="Insert" runat="server" CommandName="InsertNew" Text="" Font-Names="Tahoma" Font-Size="Small"/&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;EmptyDataTemplate&gt; &amp;nbsp;&lt;table class="style1" style="font-family: tahoma; font-size: small"&gt; &lt;tr&gt; &lt;td&gt; :&lt;/td&gt; &lt;td&gt; &lt;asp:DropDownList ID="TermNoEmpty" runat="server" AutoPostBack="True" DataSourceID="SqlDataSourcetermNo" DataTextField="MaintenanceTermNo" DataValueField="MaintenanceTermsID" dir="rtl" AppendDataBoundItems="True"&gt; &lt;asp:ListItem Selected="True" Value="0"&gt;choose&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/td&gt; &lt;td&gt; &lt;a href="PopupTerms.aspx" onclick="return popitup('PopupTerms.aspx')"&gt; &lt;/a&gt; &lt;/td&gt; &lt;td&gt; :&lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID="EmptyQty" runat="server" dir="rtl" Width="45px"&gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;td&gt; :&lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID="TextBoxTotal" runat="server" dir="rtl" ReadOnly="True" Width="45px"&gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:Button ID="ButtonMultiply" runat="server" Font-Size="Small" Text="x" CommandName="MultiplyEmpty" Font-Names="Tahoma" /&gt; &lt;asp:Button ID="NoDataInsert" runat="server" CommandName="NoDataInsert" Text="" Font-Names="Tahoma" Font-Size="Small" /&gt; &lt;/td&gt; &lt;td class="style2"&gt; &amp;nbsp;&lt;/td&gt; &lt;td class="style2"&gt; &amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/EmptyDataTemplate&gt; &lt;RowStyle BorderColor="Beige" BorderWidth="1px"&gt;&lt;/RowStyle&gt; &lt;/asp:GridView&gt;</pre> <p>&nbsp;</p> <p>Page_Load Code:</p> <pre class="prettyprint"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load LabelDate.Text = Convert.ToDateTime(DateTime.Now).ToString("dd/MMM/yyyy") If Not IsPostBack Then BindDropDownList() End If Dim connectionString As String connectionString = "Data Source=MOPWSQLDXB\MOPWDB;Initial Catalog=maintenanceDB;Integrated Security=True" Dim con As SqlConnection = New SqlConnection(connectionString) If GridViewMaintenance.Rows.Count = 0 Then Dim testddl As DropDownList = GridViewMaintenance.Controls(0).Controls(0).FindControl("TermNoEmpty") Session("TermID") = testddl.SelectedValue ElseIf GridViewMaintenance.Rows.Count &gt; 0 Then Dim testddl2 As DropDownList = GridViewMaintenance.FooterRow.FindControl("FooterTermNo") Session("TermID") = testddl2.SelectedValue End If Dim cmd As SqlCommand = New SqlCommand("SELECT Price FROM tbl_MaintenanceTerms Where MaintenanceTermsID='" + Session("TermID") + "'", con) con.Open() Dim reader = cmd.ExecuteReader() If reader.HasRows Then reader.Read() TextBoxPrice.Text = reader.GetValue(0).ToString() reader.Close() End If End Sub</pre> <p>On Row Command:</p> <pre class="prettyprint"> Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridViewMaintenance.RowCommand If e.CommandName = "MultiplyEmpty" Then Dim txtPrice As Integer = TextBoxPrice.Text Dim txtQty As TextBox = GridViewMaintenance.Controls(0).Controls(0).FindControl("EmptyQty") Dim txtTotal As TextBox = GridViewMaintenance.Controls(0).Controls(0).FindControl("TextBoxTotal") Dim itxtQty As Integer = Convert.ToInt32(txtQty.Text) txtTotal.Text = (txtQty.Text * txtPrice) ElseIf e.CommandName = "MultiplyFooter" Then Dim txtPrice As Integer = TextBoxPrice.Text Dim txtQtyFooter As TextBox = GridViewMaintenance.FooterRow.FindControl("TextBoxInsertQty") Dim txtTotalFooter As TextBox = GridViewMaintenance.FooterRow.FindControl("TextBoxInsertTotal") Dim itxtQtyFooter As Integer = Convert.ToInt32(txtQtyFooter.Text) txtTotalFooter.Text = (txtQtyFooter.Text * txtPrice) ElseIf e.CommandName = "MultiplyEdit" Then Dim txtQtyEdit As TextBox = CType(GridViewMaintenance.FindControl("TextBoxEditQty"), TextBox) Dim txtTotalEdit As TextBox = CType(GridViewMaintenance.FindControl("TextBoxEditTotal"), TextBox) Dim txtPrice As Integer = TextBoxPrice.Text txtTotalEdit.Text = (txtQtyEdit.Text * txtPrice) End If If e.CommandName = "NoDataInsert" Then Dim MaintenanceTermsID As DropDownList = TryCast(GridViewMaintenance.Controls(0).Controls(0).FindControl("TermNoEmpty"), DropDownList) Dim Quantity As TextBox = TryCast(GridViewMaintenance.Controls(0).Controls(0).FindControl("EmptyQty"), TextBox) Dim Total As TextBox = TryCast(GridViewMaintenance.Controls(0).Controls(0).FindControl("TextBoxTotal"), TextBox) Dim iMaintenanceTermsID As New SqlParameter("@MaintenanceTermsID", SqlDbType.VarChar, 30) iMaintenanceTermsID.Direction = ParameterDirection.Input iMaintenanceTermsID.Value = MaintenanceTermsID.Text insertParameters.Add(iMaintenanceTermsID) Dim iQuantity As New SqlParameter("@Quantity", SqlDbType.VarChar, 30) iQuantity.Direction = ParameterDirection.Input iQuantity.Value = Quantity.Text insertParameters.Add(iQuantity) Dim iTotal As New SqlParameter("@Total", SqlDbType.VarChar, 30) iTotal.Direction = ParameterDirection.Input iTotal.Value = Total.Text insertParameters.Add(iTotal) SqlDataSourceMaintenance.Insert() ElseIf e.CommandName = "InsertNew" Then Dim MaintenanceTermsID As DropDownList = TryCast(GridViewMaintenance.FooterRow.FindControl("FooterTermNo"), DropDownList) Dim Quantity As TextBox = TryCast(GridViewMaintenance.FooterRow.FindControl("TextBoxInsertQty"), TextBox) Dim Total As TextBox = TryCast(GridViewMaintenance.FooterRow.FindControl("TextBoxInsertTotal"), TextBox) Dim iMaintenanceTermsID As New SqlParameter("@MaintenanceTermsID", SqlDbType.VarChar, 30) iMaintenanceTermsID.Direction = ParameterDirection.Input iMaintenanceTermsID.Value = MaintenanceTermsID.Text insertParameters.Add(iMaintenanceTermsID) Dim iQuantity As New SqlParameter("@Quantity", SqlDbType.VarChar, 30) iQuantity.Direction = ParameterDirection.Input iQuantity.Value = Quantity.Text insertParameters.Add(iQuantity) Dim iTotal As New SqlParameter("@Total", SqlDbType.VarChar, 30) iTotal.Direction = ParameterDirection.Input iTotal.Value = Total.Text insertParameters.Add(iTotal) SqlDataSourceMaintenance.Insert() End If End Sub </pre> <pre class="prettyprint"> Protected Sub Sqldatasource1_Inserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs) e.Command.Parameters.Clear() For Each p As SqlParameter In insertParameters e.Command.Parameters.Add(p) Next End Sub Protected Sub Sqldatasource1_Inserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs) insertParameters.Clear() End Sub</pre> <p>&nbsp;</p> <p>SelectIndexChange (Update Session):</p> <pre class="prettyprint"> Protected Sub EditTermNo_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Dim connectionString As String connectionString = "Data Source=MOPWSQLDXB\MOPWDB;Initial Catalog=maintenanceDB;Integrated Security=True" Dim con As SqlConnection = New SqlConnection(connectionString) Dim gridViewRow As GridViewRow = CType(((CType(sender, Control)).NamingContainer), GridViewRow) Dim ddl1 As DropDownList = CType(gridViewRow.FindControl("EditTermNo"), DropDownList) Session("TermID") = ddl1.SelectedValue Dim cmd As SqlCommand = New SqlCommand("SELECT Price FROM tbl_MaintenanceTerms Where MaintenanceTermsID='" + Session("TermID") + "'", con) con.Open() Dim reader = cmd.ExecuteReader() If reader.HasRows Then reader.Read() TextBoxPrice.Text = reader.GetValue(0).ToString() reader.Close() End If End Sub</pre> <p>&nbsp;</p> 2012-03-04T04:02:47-05:004862564http://forums.asp.net/p/1776429/4862564.aspx/1?Re+GridView+Update+ProblemRe: GridView Update Problem <p>Any help please??</p> 2012-03-04T05:39:40-05:004862690http://forums.asp.net/p/1776429/4862690.aspx/1?Re+GridView+Update+ProblemRe: GridView Update Problem <p>Hi,</p> <p>Don't worry.</p> <p>You have to gover proper order to the update parameters and remove braces from the where clause.</p> <pre class="prettyprint">UpdateCommand=&quot;Update tbl_MaintenanceSubTemp SET Quantity=@Quantity, MaintenanceTermsID=@MaintenanceTermsID, Total=@Total where (MaintenanceSubTempID=@MaintenanceSubTempID)&quot; Change this to : UpdateCommand=&quot;Update tbl_MaintenanceSubTemp SET Quantity=@Quantity, MaintenanceTermsID=@MaintenanceTermsID, Total=@Total where MaintenanceSubTempID=@MaintenanceSubTempID&quot; &lt;UpdateParameters&gt; &lt;asp:Parameter Name=&quot;Quantity&quot; /&gt; &lt;asp:Parameter Name=&quot;MaintenanceTermsID&quot; /&gt; &lt;asp:Parameter Name=&quot;Total&quot; /&gt; &lt;asp:Parameter Name=&quot;MaintenanceSubTempID&quot; /&gt; &lt;/UpdateParameters&gt;</pre> <p></p> <p></p> <p>tHis will work for you.</p> 2012-03-04T09:15:48-05:004863280http://forums.asp.net/p/1776429/4863280.aspx/1?Re+GridView+Update+ProblemRe: GridView Update Problem <p></p> <blockquote><span class="icon-blockquote"></span> <h4>lizareay</h4> <p></p> <p>Hi,</p> <p>Don't worry.</p> <p>You have to gover proper order to the update parameters and remove braces from the where clause.</p> <pre class="prettyprint">UpdateCommand=&quot;Update tbl_MaintenanceSubTemp SET Quantity=@Quantity, MaintenanceTermsID=@MaintenanceTermsID, Total=@Total where (MaintenanceSubTempID=@MaintenanceSubTempID)&quot; Change this to : UpdateCommand=&quot;Update tbl_MaintenanceSubTemp SET Quantity=@Quantity, MaintenanceTermsID=@MaintenanceTermsID, Total=@Total where MaintenanceSubTempID=@MaintenanceSubTempID&quot; &lt;UpdateParameters&gt; &lt;asp:Parameter Name=&quot;Quantity&quot; /&gt; &lt;asp:Parameter Name=&quot;MaintenanceTermsID&quot; /&gt; &lt;asp:Parameter Name=&quot;Total&quot; /&gt; &lt;asp:Parameter Name=&quot;MaintenanceSubTempID&quot; /&gt; &lt;/UpdateParameters&gt;</pre> <p></p> <p></p> <p>tHis will work for you.</p> <p></p> </blockquote> <p></p> <p>Thank you for your reply,</p> <p>but still I have the same problem, the DropDownList becomes NULL in the database :(</p> 2012-03-05T03:32:51-05:004873055http://forums.asp.net/p/1776429/4873055.aspx/1?Re+GridView+Update+ProblemRe: GridView Update Problem <p>Hi,</p> <p>If you are lost the data in the gridview when you update it you can try to rebind it with the database.</p> <p>You can learn the samples in the below links. I think they will give you some help.</p> <p><a href="http://www.codeproject.com/Articles/24741/Nesting-the-DropDownList-to-Gridview-in-ASP-NET-2">http://www.codeproject.com/Articles/24741/Nesting-the-DropDownList-to-Gridview-in-ASP-NET-2</a></p> <p><a href="http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET-2-0">http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET-2-0</a></p> <p><a href="http://msdn.microsoft.com/en-us/library/ms178294.aspx">http://msdn.microsoft.com/en-us/library/ms178294.aspx</a></p> 2012-03-10T12:19:55-05:004874931http://forums.asp.net/p/1776429/4874931.aspx/1?Re+GridView+Update+ProblemRe: GridView Update Problem <p>Hi all,</p> <p>The problem is, once I click on the Edit button, it refresh the dropdownlist from the selected value ex. 10034 to &quot;Choose&quot;</p> <p>How can I solve this problem.??</p> <p>the dropdownlist should keep the same selected value</p> 2012-03-12T09:11:48-04:004876567http://forums.asp.net/p/1776429/4876567.aspx/1?Re+GridView+Update+ProblemRe: GridView Update Problem <p>Can anyone help me pleaase!!!</p> 2012-03-13T04:38:53-04:004876981http://forums.asp.net/p/1776429/4876981.aspx/1?Re+GridView+Update+ProblemRe: GridView Update Problem <p>I want to clarify something, when I click on the edit button, all colomns retrive the saved data. but the dropdownlist colomn retrives the first value which is &quot;Choose&quot;</p> <p>How can I solve this problem?</p> <p>Please I need your help</p> 2012-03-13T08:15:36-04:00