Jump to content

putting data into datagrid


silversasoriza

Recommended Posts

Hi, im still new with the .net tech.... im doin a project and i keep on getting this error....

System.FormatException: Input string was not in a correct format. at System.Number.ParseDecimal(String s, NumberStyles style, NumberFormatInfo info) at System.Decimal.Parse(String s, NumberStyles style, IFormatProvider provider) at System.Convert.ToDecimal(String value, IFormatProvider provider) at System.String.System.IConvertible.ToDecimal(IFormatProvider provider) at System.Convert.ToDecimal(Object value) at System.Data.Common.DecimalStorage.Set(Int32 record, Object value) at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <> in price Column. Expected type is Decimal.

i uses 2 datagrid: DGProduct and sgShopCart and this is my code:

Imports System.Data.OleDbImports homepage.configuration1Public Class ProductCatalog_Product_Giraffe    Inherits System.Web.UI.Page    Dim ds As DataSet    Protected WithEvents dgShopCart As System.Web.UI.WebControls.DataGrid    Protected WithEvents lbTotal As System.Web.UI.WebControls.Label    Dim cart As DataTablePrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        'Put user code to initialize the page here        Dim config As New homepage.configuration1        ds = config.load_data("select prodImage, prodDesc, prodPrice from product where prodType = 'PlushToy-Giraffe'")        bind_data()        shop_cart()    End Sub    Private Sub DGProductGiraffe_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DGProductGiraffe.ItemDataBound        Dim img As System.Web.UI.WebControls.Image        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then            img = CType(e.Item.Cells(1).Controls(1), System.Web.UI.WebControls.Image)            img.ImageUrl = e.Item.Cells(0).Text        End If    End Sub    Private Sub bind_data()        DGProductGiraffe.DataSource = ds        DGProductGiraffe.DataBind()    End Sub    Public Sub grid_cartcommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)        Dim dr As DataRow = cart.NewRow()        Dim drcart As DataRow        Dim blMatch As Boolean = False        Dim iCount As Integer = 0        Dim itemCell As TableCell = e.Item.Cells(1)        Dim item As String = itemCell.Text        itemCell = e.Item.Cells(3)        Dim price As String = itemCell.Text        If e.CommandName = "buy" Then            'check whether product already in shopping cart, if not add new row, else update row            For Each drcart In cart.Rows                blMatch = False                If drcart("item") = item Then                    'update                    drcart("qty") += 1                    drcart("total") = drcart("qty") * drcart("price")                    blMatch = True                    Exit For                End If            Next            If Not blMatch Then                dr(0) = item                dr(1) = price                dr(2) = 1                dr(3) = price                cart.Rows.Add(dr)            End If        ElseIf e.CommandName = "DeleteFromCart" Then            For Each drcart In cart.Rows                blMatch = False                If drcart("item") = item Then                    cart.Rows(iCount).Delete()                    Exit For                End If                iCount += 1            Next        End If        dgShopCart.DataBind()        calcTotal()    End Sub    Sub calcTotal()        Dim dTotal As Decimal        Dim drcart As DataRow        'calc total for all rows in shopcart        For Each drcart In cart.Rows            dTotal += drcart("total")        Next        lbTotal.Text = dTotal.ToString()    End Sub    Private Sub shop_cart()        If Session.Item("cart") Is Nothing Then            cart = New DataTable            cart.Columns.Add(New DataColumn("item", GetType(String)))            cart.Columns.Add(New DataColumn("price", GetType(Decimal)))            cart.Columns.Add(New DataColumn("prodQtyIn", GetType(Integer)))            cart.Columns.Add(New DataColumn("total", GetType(Decimal)))            Session.Item("cart") = cart        Else            cart = Session.Item("cart")        End If        dgShopCart.DataSource = cart        dgShopCart.DataBind()    End SubEnd Class

please teach me how to solve this error... thxsilversasoriza

Link to comment
Share on other sites

from what I can tell this is the source of your error

cart.Columns.Add(New DataColumn("price", GetType(Decimal)))

You are adding a datacolumn to cart but the datacolumn value is "price" and you're trying to convert it to decimal (which you can't).I think that is the problem.

Link to comment
Share on other sites

from what I can tell this is the source of your error
cart.Columns.Add(New DataColumn("price", GetType(Decimal)))

You are adding a datacolumn to cart but the datacolumn value is "price" and you're trying to convert it to decimal (which you can't).I think that is the problem.

But when i try it in other project the code works perfectly fine... does the decimal got something to do with the datatype in the database??thx
Link to comment
Share on other sites

one of the records that is trying to go into the price column is not in deciaml format or can't be converted. Make sure it is not a string or is not null.

Thx, i try to change the datatype to string n worked completely fine... however when i try to change the button type in the datagrid property builder from link button to push button... nothing happen... is there a different coding between the two button??thx =)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...