Solved

Bulk update on a gridview with a checkbox gives error

Posted on 2006-10-24
6
723 Views
Last Modified: 2012-08-13
I'm using the code from http://msdn2.microsoft.com/en-us/library/ms404814.aspx to do a bulk update on a datagrid. Basicly you put text boxes in the item view, then click an update button which saves the data to a table scan trhough it to see if a row has changed then does a SQL update. As I'm trying to do this I have one setting that is a check box. When I try to do a row update that gives me an error that it can't save null into a boolean value. I've got it set correctly in the upade parameters and everything as a boolean. What am I messing up?

Protected Sub UpdateButton_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
        originalDataTable = CType(ViewState("originalValuesDataTable"), System.Data.DataTable)

        For Each r As GridViewRow In GridView1.Rows
            If IsRowModified(r) Then GridView1.UpdateRow(r.RowIndex, False) <-- can't save a null to a bit in SQL????
        Next

        ' Rebind the Grid to repopulate the original values table.
        tableCopied = False
        GridView1.DataBind()
End Sub

Protected Function IsRowModified(ByVal r As GridViewRow) As Boolean
        Dim currentID As Integer
        Dim currentQuantity As String
        Dim currentsellprice As String
        Dim currentextprice As String
        Dim currentUnitCost As String
        Dim currentSystemSale As Boolean

        currentID = Convert.ToInt32(GridView1.DataKeys(0).Value)

        currentQuantity = CType(r.FindControl("QuantityTextBox"), TextBox).Text
        currentsellprice = CType(r.FindControl("sellpriceTextBox"), TextBox).Text
        currentextprice = CType(r.FindControl("extpriceTextBox"), TextBox).Text
        currentUnitCost = CType(r.FindControl("UnitCostTextBox"), TextBox).Text
        currentSystemSale = CType(r.FindControl("chkSystemSale"), CheckBox).Checked

        Dim row As System.Data.DataRow = originalDataTable.Select(String.Format("OrderDetailID = {0}", currentID))(0)

        If Not currentQuantity.Equals(row("Quantity").ToString()) Then Return True
        If Not currentsellprice.Equals(row("sellprice").ToString()) Then Return True
        If Not currentextprice.Equals(row("extprice").ToString()) Then Return True
        If Not currentUnitCost.Equals(row("UnitCost").ToString()) Then Return True
        If Not currentSystemSale.Equals(row("SystemSale").ToString()) Then Return True

        Return False
End Function

0
Comment
Question by:kevlause
6 Comments
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 17795430
Did you try
If IsRowModified(r) Then GridView1.UpdateRow(r.RowIndex, 0)

?
0
 

Author Comment

by:kevlause
ID: 17795521
I still get...
Cannot insert the value NULL into column 'SystemSale', table 'dbPoints.dbo.OrderDetails'; column does not allow nulls. UPDATE fails.
The statement has been terminated.

The false is if you want to cause validation or not I thought it was an issue with myupdate statement, but I think I have that right. I set the select command in the page load section.

                            <asp:SqlDataSource ID="SqlDataSource7" runat="server" ConnectionString="<%$ ConnectionStrings:dbPointsConnectionString %>" DeleteCommand="DELETE FROM [OrderDetails] WHERE [OrderDetailID] = @OrderDetailID" UpdateCommand="UPDATE [OrderDetails] SET [Quantity] = @Quantity, [SellPrice] = @SellPrice, [UnitCost] = @UnitCost, [SystemSale] = @SystemSale WHERE [OrderDetailID] = @OrderDetailID" >
                                <DeleteParameters>
                                    <asp:Parameter Name="OrderDetailID" Type="Int32" />
                                </DeleteParameters>
                                <UpdateParameters>
                                    <asp:Parameter Name="Quantity" Type="Double" />
                                    <asp:Parameter Name="SellPrice" Type="Decimal" />
                                    <asp:Parameter Name="UnitCost" Type="Decimal" />
                                    <asp:Parameter Name="SystemSale" Type="Boolean" />
                                    <asp:Parameter Name="OrderDetailID" Type="Int32" />
                                </UpdateParameters>
                             </asp:SqlDataSource>
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17802282
How is the GridView bound?  Have you defined columns?  Or are they auto-generated?  Can you show the columns for the GridView?

Bob
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:kevlause
ID: 17802457
I just figured this out another way. I think the problem was I was setting the select command on page load and it was messing with the way the datasource was working. I ended up just writing a sql commmand o write to the database to get around it. Thanks for the post!
0
 

Author Comment

by:kevlause
ID: 17802478
   Protected Function IsRowModified(ByVal r As GridViewRow) As Boolean

        Dim currentID As Integer
        Dim currentQuantity As String
        Dim currentsellprice As String
        Dim currentUnitCost As String
        Dim currentSystemSale As Boolean
        currentID = Convert.ToInt32(GridView1.DataKeys(Session("scan")).Value)
        currentQuantity = CType(r.FindControl("QuantityTextBox"), TextBox).Text
        currentsellprice = CType(r.FindControl("sellpriceTextBox"), TextBox).Text
        currentUnitCost = CType(r.FindControl("UnitCostTextBox"), TextBox).Text
        currentSystemSale = CType(r.FindControl("chkSystemSale"), CheckBox).Checked

        Dim row As System.Data.DataRow = originalDataTable.Select(String.Format("OrderDetailID = {0}", currentID))(0)

        If Not currentQuantity.Equals(row("Quantity").ToString()) Then Return True
        If Not currentsellprice.Equals(row("sellprice").ToString())  Then Return True
        If Not currentUnitCost.Equals(String.Format("{0:c}", row("UnitCost").ToString())) Then Return True
        If Not currentSystemSale.Equals(row("SystemSale").ToString()) Then Return True
        Return False
    End Function

    Protected Sub UpdateButton_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
        originalDataTable = CType(ViewState("originalValuesDataTable"), System.Data.DataTable)
        Session("scan") = 0
        For Each r As GridViewRow In GridView1.Rows
            If IsRowModified(r) = True Then
                Dim currentID As Integer
                Dim currentQuantity As String
                Dim currentsellprice As String
                Dim currentUnitCost As String
                Dim currentSystemSale As Boolean
                currentID = Convert.ToInt32(GridView1.DataKeys(Session("scan")).Value)
                Dim row As System.Data.DataRow = originalDataTable.Select(String.Format("OrderDetailID = {0}", currentID))(0)
                currentQuantity = CType(r.FindControl("QuantityTextBox"), TextBox).Text
                currentsellprice = CType(r.FindControl("sellpriceTextBox"), TextBox).Text
                currentUnitCost = CType(r.FindControl("UnitCostTextBox"), TextBox).Text
                currentSystemSale = CType(r.FindControl("chkSystemSale"), CheckBox).Checked
                Dim sqlcmd As String = "UPDATE OrderDetails SET Quantity = '" & currentQuantity & "', SellPrice = convert(money,'" & currentsellprice & "'), UnitCost = convert(money,'" & currentUnitCost & "'), SystemSale = '" & System.Convert.ToInt32(currentSystemSale) & "' WHERE OrderDetailID = '" & currentID & "'"
                Dim connectionString As String = WebConfigurationManager.ConnectionStrings("dbPointsConnectionString").ConnectionString
                Dim conn As New SqlConnection(connectionString)
                Dim cmd As New SqlCommand(sqlcmd, conn)
                Using conn
                    conn.Open()
                    cmd.ExecuteScalar()
                End Using
            End If
            Session("scan") = Session("scan") + 1
        Next
        Session("scan") = 0
        ' Rebind the Grid to repopulate the original values table.
        tableCopied = False
        GridView1.DataBind()
    End Sub
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 17816877
Asker resolved question - the 500 points have been refunded and the question
PAQ'd

Regards,
RomMod
Community Support Moderator
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now