Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I need help how we can fix a problem when my query contain a ' in column value

foreach (var item in lstVendorNames)
                {
                    var lstQuery = "select * from Transactions where MasterId not in (Select masterId from BillDetails) and MasterId in (select MasterId from Transactions where Ledger='" + item + "') and Ledger= '" + item + "'";
                    SqlDataAdapter ada = new SqlDataAdapter(lstQuery, CGlobalTally_MT.sqlConnection);
                    DataTable dtvendBill = new DataTable();
                    ada.Fill(dtvendBill);




select * from Transactions where MasterId not in (Select masterId from BillDetails) and MasterId in (select MasterId from Transactions where Ledger='Aerostar's Security Group') and Ledger= 'Aerostar's Security Group'



I need help how to fix it it is coming for two vendors

What I have tried:

I tried to remove prefix but could not solved it
Posted
Updated 31-Jan-17 22:54pm
v2

1 solution

First of all, your code is vulnerable to SQL Injection.[^]
And good news is, if you take care of prevention for SQL Injection, it will automatically solve your issue too.

Either you can create a stored procedure or consider creating parameterised query something like following-
C#
var lstQuery = @"select * from Transactions where MasterId not in (Select masterId from BillDetails) and MasterId in (select MasterId from Transactions where Ledger=@Ledger) and Ledger= @Ledger";
SqlCommand cmd = new SqlCommand(lstQuery, CGlobalTally_MT.sqlConnection);
cmd.Parameters.AddWithValue("@Ledger", item);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataTable dtvendBill = new DataTable();
ada.Fill(dtvendBill);


Hope, it helps :)
 
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