Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an Store proc in which I am getting the name of the company , One of the Company has the apostrophe For Example- custom's but in Server side i am getting has custom & # 3 9; s , hence it is giving me error


public ActionResult OfferDocument()
        {
            List<IIFL_IB_Prospects> lstProspects = new List<IIFL_IB_Prospects>();
            InvestmentBankingParent IBParent = new InvestmentBankingParent();
            SessionParams.IBRenderMode = "0";
            DataSet ds = new DataSet();
            IIFL_IB_Prospects IIFL_IB_Prospects;
            setUserName();
            try
            {
                ds = getProspects();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    IIFL_IB_Prospects = new IIFL_IB_Prospects();
                    IIFL_IB_Prospects.CompanyName = Server.HtmlEncode(Convert.ToString(dr["companyname"]));
                    lstProspects.Add(IIFL_IB_Prospects);
                }


                

                IBParent.Prospects = lstProspects;
                IBParent.recentTransaction = getKeyTransaction();
                IBParent.testimonials = getClientTestimonials();
            }
            catch (Exception ex)
            {
                string msg = ex.Message + " at " + DateTime.Now + " From RecentTransaction";
                LogObj.writeLog("ErrorLogIB.txt", msg);              
            }

            obj.trackUser("View Investment Banking/Offer document");
            //return View(new IIFL_IB_Prospects(lstProspects));
            return View(IBParent);
        }


What I have tried:

I have
IIFL_IB_Prospects.CompanyName=Server.HtmlEncode(Convert.ToString(dr["companyname"]));
Posted
Updated 17-Apr-17 23:54pm
v4

1 solution

Probably - and without seeing the code you use to access or store the data into the DB we can't tell for sure - it's down to you making a huge mistake, and leaving your DB wide open to SQL Injection. When you concatenate strings to form an SQL command:
C#
string sql = "INSERT INTO MyTable (CompanyName) VALUES ('" + txtCompanyName.Text + "')";
You are both wide open to the user doing what he likes with your DB - including deleting it - just by typing in a textbox, and to problems like this, where the apostrophe in the company name terminates the SQL string and causes problems.

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Chances are you other problems will go away at the same time, once you fix your DB data as well.
 
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