C# Special characters(', /, ) to SQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dkate777
    New Member
    • Jun 2007
    • 18

    C# Special characters(', /, ) to SQL

    HI,

    I try to save data to SQL Server 2005 and it's not pick up some special characters, specialy single quetes .
    Is anybody on my code sample can show me how to do this

    Thanks

    This is my code sample:

    protected void btnSubmit_Click (object sender, EventArgs e)
    {
    try
    {
    int userId = Convert.ToInt32 (Session["LoggedUser Id"]);
    if (userId == 0)
    {
    lblError.Text = "Your Session Expired. Please Login Again.";
    return;
    }
    UserBO objUserBO = new UserBO();
    objUserBO.UserI d = Convert.ToInt32 (Session["LoggedUser Id"]);
    objUserBO.HeadL ine = txtHeadLine.Tex t;
    objUserBO.First Name = txtFirstName.Te xt;
    objUserBO.LastN ame = txtLastName.Tex t;
    objUserBO.Addre ss = txtAddress.Text ;
    objUserBO.City = txtCity.Text;
    objUserBO.State = txtState.Text;
    objUserBO.ZipCo de = txtZipCode.Text ;
    objUserBO.About Me = txtAboutMe.Text ;
    objUserBO.Phone = txtPhone.Text;
    objUserBO.Websi te = txtWebsite.Text ;
    objUserBO.Blog = txtBlog.Text;
    objUserBO.Busin essProfileLink = lblBusinessProf ileLink.Text;
    objUserBO.MySer viceInfo = txtServiceInfo. Text;
    objUserBO.MySer viceValueReason = txtServiceValue Reason.Text;
    objUserBO.MoreP icsLink = txtPicturesLink .Text;
    objUserBO.Video Link = txtVideosLink.T ext;
    objUserBO.Categ ory = ddCategory.Sele ctedItem.ToStri ng();
    objUserBO.SubCa tegory = ddSubCategory.S electedItem.ToS tring();
    objUserBO.Searc hKeywords = txtSearchKeywor ds.Text.ToStrin g();
    objUserBO.Busin essName = txtBusinessName .Text.ToString( );
    objUserBO.Speci alOffer = txtSpecialOffer .Text;

    bool success = objUserDL.Updat eUser(objUserBO );
    if (success)
    {
    lblError.Text = "Profile Info Successfully Updated.";
    btnPreview.Enab led = true;
    }
    else
    lblError.Text = "Profile Info Could Not Be Updated. Please Try Later";
    }
    catch (Exception ex)
    {
    lblError.Text = "Profile Info Could Not Be Updated. Please Try Later";

    }

    }
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    i presume you are creating sql statement and feeding user input as strings to ure sql command

    n that case have a look at this and this

    Comment

    • dkate777
      New Member
      • Jun 2007
      • 18

      #3
      first link it's not working

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Both links work fine.

        As for the single quote, simply replace every occurance of a single quote with TWO single quotes

        Comment

        Working...