sql - .Conversion error on unique identifier, shouldn't be referenced at all? -
i'm working on shopping cart website, have function retrieves voucher database:
public shared function checkforvoucher(byval strvouchername string) datatable dim connect new sqlconnection dim data new datatable ''connection works, finds number of vouchers in db match either code or id. id used randomly generated vouchers. connect.connectionstring = "server = server-sql01; trusted_connection=yes; database=pcsql" connect.open() dim query string dim search string search = strvouchername if search.length >= 25 query = "select * pcsql.dbo.x_ww_voucher_details vid='" & search & "' " else query = "select * pcsql.dbo.x_ww_voucher_details vouchername='" & search & "' " end if dim command = new sqldataadapter(query, connect) command.fill(data) connect.close() strvouchername = query return data end function
this works peachy-keen on site i'm working on. @ end of customers order fires function, increase totaluses of voucher 1 (my boss wants make sure people don't spam codes , run him out of business).
public shared function adduse(byval strvouchername string, intcurrentuses integer) boolean dim connect new sqlconnection dim data new datatable connect.connectionstring = "server = server-sql01; trusted_connection = yes; database=pcsql" connect.open() dim query string dim name string = strvouchername dim current integer = intcurrentuses dim newint integer = current + 1 query = "update dbo.x_ww_voucher_details set currentuses= @currentuses vouchername=@vouchername" dim command new sqlcommand(query, connect) command.parameters.add("@vouchername", sqldbtype.varchar) command.parameters("@vouchername").value = name command.parameters.add("@currentuses", sqldbtype.int) command.parameters("@currentuses").value = newint command.executenonquery() connect.close() return true end function
this works fine, , can check sql table in sql server , see "currentuses" column has increased one. yay.
problem
when test code submitting again, fails if currentuses column equal validuses column. end error "conversion failed when converting character string uniqueidentifier" though unique identifier not directly referenced @ all.
i've tested on voucher valid 5 times. times 1 through 4, no errors currentuses 5... no go.
why equal columns cause function crap itself?
sql table
-vid uniqueidentifier -vouchername nchar(15) -expirydate date -validuses int -currentuses int -discounttype nchar(15) -appliesto nchar(15) -numberof nchar(20) -amount int -noofitems int -category nchar(100) -freebieid nchar(15) -discountamount int -description nchar(255)
edit
i have narrowed error down if statement.
if search.length >= 25 query = "select * pcsql.dbo.x_ww_voucher_details vid='" & search & "' " else query = "select * pcsql.dbo.x_ww_voucher_details vouchername='" & search & "' " end if
the voucher called cart.aspx using
protected sub btnvouchercheck_click(byval sender object, byval e system.eventargs) handles btnvouchercheck.click dim data datatable = voucher.checkforvoucher(txtvoucher.text) if data.rows.count > 0 dim row datarow row = data.rows(0) voucher.voucherid = row.item("vid").tostring().trim() voucher.vouchername = row.item("vouchername").tostring().trim() voucher.expirydate = row.item("expirydate") voucher.validuses = row.item("validuses") voucher.currentuses = row.item("currentuses") voucher.discounttype = row.item("discounttype").tostring().trim() voucher.appliesto = row.item("appliesto").tostring().trim() voucher.numberof = row.item("numberof").tostring().trim() voucher.amount = row.item("amount") voucher.noofitems = row.item("noofitems") voucher.category = row.item("category").tostring().trim() voucher.freebieid = row.item("freebieid").tostring().trim() voucher.discountamount = row.item("discountamount") dim count int32 count = 0 dim expiry datetime = voucher.expirydate dim today datetime = date.today() count = ((expiry - today).days) if count <= -1 txtvoucher.text = "voucher expired" else 'further checks. one: valid uses against current uses, checks if many people have used voucher. if (voucher.currentuses >= voucher.validuses) txtvoucher.text = "sorry, voucher no longer valid" else 'redirects 1 of 3 methods based on discount type. c.vouchername = voucher.vouchername if c.discounted = true each item repeateritem in rptcart.items dim quantity textbox = ctype(item.findcontrol("txtquantity"), textbox) dim productid hyperlink = ctype(item.findcontrol("hlproductid"), hyperlink) dim lblprice label = ctype(item.findcontrol("lblprice"), label) lblprice.text = product.getproductprice(productid.text, quantity.text, c) next end if select case voucher.discounttype case "dollar" txtvoucher.text = "$ not yet supported" case "percentage" percentagediscount() case "freebie" txtvoucher.text = "freebie not yet supported" case else txtvoucher.text = "not working." end select end if end if else end if end sub
the 2 relevant columns compared in middle of function, code doesn't execute far if equal being with. conversion error referencing line in checkforvoucher function pasted above. can't comparison causes error because datatable use doesn't exist @ point error occurs.
the problem have when value of search
25 characters or longer, passing database compare vid
column. column uniqueidentifier
means it's guid in c# terms. if value pass in cannot converted directly guid, conversion error.
Comments
Post a Comment