Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone...

I am getting a data from checklistbox into a string and pass that string into a sql query for in operator.But My coding is not working.I show you.

SQL
CheckedInvoiceNo = ""
       'Dim ff As New frmSalePerformaReport
       For I = 0 To chklistbx.Items.Count - 1
           If chklistbx.GetItemChecked(I) = True Then
               CheckedInvoiceNo = CheckedInvoiceNo + "," + chklistbx.Items(I).ToString()
           End If
       Next
       sql = "select PFDate,PFNo,CustType,Party,PartyCode,SaleNo,SaleDate,InvHeading,SaleType,Mode,Transporter from sale where PFNo in '" & CheckedInvoiceNo & "'"

------------------------
Edit: Added information that OP originally submitted as a solution.
Quote:
I am getting data into checkinvoiceNo.But My syntax of append a string is not correct

I am getting like that checkinvoice no = NZ-SP/13-14/00002 ,NZ-SP/13-14/00001 ,

But I wanna get like that checkinvoice no = 'NZ-SP/13-14/00002' , 'NZ-SP/13-14/00001'
Posted
Updated 1-Jan-14 8:55am
v2
Comments
TnTinMn 1-Jan-14 14:57pm    
I have added the information that your erroneously submitted as a solution to your question. Please delete "Solution 2" that you posted.
Thanks.

First of all, you should not "inject" value directly into your sql query like what you did
SQL
in '" & CheckedInvoiceNo & "'"
to avoid sql injection, read more about it here SQL Injection Attacks and Some Tips on How to Prevent Them[^].

Secondly, you sql query is not constructed correctly, check these out:
1. SQL Select[^]
2. SQL IN[^]
 
Share this answer
 
Quote:
I am getting data into checkinvoiceNo.But My syntax of append a string is not correct

I am getting like that checkinvoice no = NZ-SP/13-14/00002 ,NZ-SP/13-14/00001 ,

But I wanna get like that checkinvoice no = 'NZ-SP/13-14/00002' , 'NZ-SP/13-14/00001'

I would recommend that you use a StringBuilder to construct "CheckedInvoiceNo". Something like this:
VB
Dim CheckedInvoiceNo As New System.Text.StringBuilder()
For Each CheckedItem As Object In chklistbx.CheckedItems
   If CheckedInvoiceNo.Length > 0 Then ' a prior item was added, so add a comma
      CheckedInvoiceNo.Append(","c)
   End If
   CheckedInvoiceNo.Append("'"c)
   CheckedInvoiceNo.Append(CheckedItem.ToString())
   CheckedInvoiceNo.Append("'"c)
Next

' note: I modified the "In" expresion
sql = "select PFDate,PFNo,CustType,Party,PartyCode,SaleNo,SaleDate,InvHeading,SaleType,Mode,Transporter from sale where PFNo IN (" & CheckedInvoiceNo.ToString() & ")"


Also, please pay heed to Pete Leow's comments about parameterized queries.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900