Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. sql server insert

sql server insert

Scheduled Pinned Locked Moved Database
databasehelpsql-serversysadmin
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    msomar
    wrote on last edited by
    #1

    I have a TexBox and it's value stored in a database. but whwe the entered value like ' , ' or ' ; ' there is some error occurred ,so how I can solve this problem.

    L M 2 Replies Last reply
    0
    • M msomar

      I have a TexBox and it's value stored in a database. but whwe the entered value like ' , ' or ' ; ' there is some error occurred ,so how I can solve this problem.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      that should not happen. assuming your DB field is nvarchar, it should be able to hold whatever text you enter. what is your database? are you using SQLParameter? maybe best show us your relevant code. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      M 1 Reply Last reply
      0
      • L Luc Pattyn

        that should not happen. assuming your DB field is nvarchar, it should be able to hold whatever text you enter. what is your database? are you using SQLParameter? maybe best show us your relevant code. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        M Offline
        M Offline
        msomar
        wrote on last edited by
        #3

        OK,

        protected void Button1_Click(object sender, EventArgs e)
        {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["AdNetConnectionString"].ToString();

                Session\["CampaignName"\]= TexBox1.Text.ToString();
                string sql;
                sql = "insert into AdCampaign (CampaignName)values( ' " + Session\["CampaignName"\] + " ');
                SqlCommand cmd = new SqlCommand(sql, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
        }
        
        L B 2 Replies Last reply
        0
        • M msomar

          OK,

          protected void Button1_Click(object sender, EventArgs e)
          {
          SqlConnection con = new SqlConnection();
          con.ConnectionString = ConfigurationManager.ConnectionStrings["AdNetConnectionString"].ToString();

                  Session\["CampaignName"\]= TexBox1.Text.ToString();
                  string sql;
                  sql = "insert into AdCampaign (CampaignName)values( ' " + Session\["CampaignName"\] + " ');
                  SqlCommand cmd = new SqlCommand(sql, con);
                  con.Open();
                  cmd.ExecuteNonQuery();
                  con.Close();
          }
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          I still don't know what your database is. I see you are not using SQLParameter; it would almost certainly solve your problem. Read up on it, and use it; it will keep a lot of problems away, SQL injection vulnerability being one of them. I would not move the data from the textbox through the session array into the SQL command; no need for such a detour. No need for ToString() either, the content of a TextBox is always a string. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          1 Reply Last reply
          0
          • M msomar

            OK,

            protected void Button1_Click(object sender, EventArgs e)
            {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["AdNetConnectionString"].ToString();

                    Session\["CampaignName"\]= TexBox1.Text.ToString();
                    string sql;
                    sql = "insert into AdCampaign (CampaignName)values( ' " + Session\["CampaignName"\] + " ');
                    SqlCommand cmd = new SqlCommand(sql, con);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
            }
            
            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            How to use a parameterized query, using your code as an example:

            sql = "insert into AdCampaign (CampaignName) values(@myParameter)";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("myParameter", Session["CampaignName"]);

            I.e. in your sql statement, no values are shown, but only the parameters; with SQL Server, parameter names use to start with an @. After having created the command object, you add all of your parameters with their respective values.

            M 1 Reply Last reply
            0
            • M msomar

              I have a TexBox and it's value stored in a database. but whwe the entered value like ' , ' or ' ; ' there is some error occurred ,so how I can solve this problem.

              M Offline
              M Offline
              Md Marufuzzaman
              wrote on last edited by
              #6

              I always prefer of using storedProcedure with I/O parama instant of hard coded sql statement.

              Thanks Md. Marufuzzaman


              I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

              1 Reply Last reply
              0
              • B Bernhard Hiller

                How to use a parameterized query, using your code as an example:

                sql = "insert into AdCampaign (CampaignName) values(@myParameter)";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("myParameter", Session["CampaignName"]);

                I.e. in your sql statement, no values are shown, but only the parameters; with SQL Server, parameter names use to start with an @. After having created the command object, you add all of your parameters with their respective values.

                M Offline
                M Offline
                msomar
                wrote on last edited by
                #7

                ok , my code is:

                sql_Store_Sessions = "insert into AdCampaign (AccountID,AdType,SiteCategory,Language,Gender,Age,Keyword,MaxBudget,DailyBudget,CampaignName,AdHeadline,AdText,DisplayUrl,TargetUrl,BannerImg,ImgName,Date,Status)values(" + Session["SelectIdFromTableAccount"] + ",'" + Session["adType"] + "','" + Session["Category"] + "','" + Session["Language"] + "','" + Session["Gender"] + "','" + Session["Age"] + "',@AdvKeyword," + Session["MaxBudget"] + "," + Session["Daily_Budget"] + ",@CampaignName,@AdHeadLine,@AdText,@DisplayUrl,@DestinationUrl,@Banner_Img,@Img_Name,'" + DateTime.Now + "','Paused')";
                SqlCommand cmd_Store_Sessions = new SqlCommand(sql_Store_Sessions, con);
                cmd_Store_Sessions.Parameters.AddWithValue("CampaignName", Session["CampaignName"]);
                cmd_Store_Sessions.Parameters.AddWithValue("AdHeadLine", Session["AdHeadLine"]);
                cmd_Store_Sessions.Parameters.AddWithValue("AdText", Session["AdText"]);
                cmd_Store_Sessions.Parameters.AddWithValue("DisplayUrl", Session["DisplayUrl"]);
                cmd_Store_Sessions.Parameters.AddWithValue("DestinationUrl", Session["DestinationUrl"]);
                cmd_Store_Sessions.Parameters.AddWithValue("Banner_Img", Session["Banner_Img"]);
                cmd_Store_Sessions.Parameters.AddWithValue("Img_Name", Session["Img_Name"]);

                        con.Open();
                        cmd\_Store\_Sessions.ExecuteNonQuery();
                        con.Close();
                

                but there is an error : The parameterized query '(@AdvKeyword nvarchar(2),@CampaignName nvarchar(2),@AdHeadLine n' expects the parameter '@Banner_Img', which was not supplied

                T 1 Reply Last reply
                0
                • M msomar

                  ok , my code is:

                  sql_Store_Sessions = "insert into AdCampaign (AccountID,AdType,SiteCategory,Language,Gender,Age,Keyword,MaxBudget,DailyBudget,CampaignName,AdHeadline,AdText,DisplayUrl,TargetUrl,BannerImg,ImgName,Date,Status)values(" + Session["SelectIdFromTableAccount"] + ",'" + Session["adType"] + "','" + Session["Category"] + "','" + Session["Language"] + "','" + Session["Gender"] + "','" + Session["Age"] + "',@AdvKeyword," + Session["MaxBudget"] + "," + Session["Daily_Budget"] + ",@CampaignName,@AdHeadLine,@AdText,@DisplayUrl,@DestinationUrl,@Banner_Img,@Img_Name,'" + DateTime.Now + "','Paused')";
                  SqlCommand cmd_Store_Sessions = new SqlCommand(sql_Store_Sessions, con);
                  cmd_Store_Sessions.Parameters.AddWithValue("CampaignName", Session["CampaignName"]);
                  cmd_Store_Sessions.Parameters.AddWithValue("AdHeadLine", Session["AdHeadLine"]);
                  cmd_Store_Sessions.Parameters.AddWithValue("AdText", Session["AdText"]);
                  cmd_Store_Sessions.Parameters.AddWithValue("DisplayUrl", Session["DisplayUrl"]);
                  cmd_Store_Sessions.Parameters.AddWithValue("DestinationUrl", Session["DestinationUrl"]);
                  cmd_Store_Sessions.Parameters.AddWithValue("Banner_Img", Session["Banner_Img"]);
                  cmd_Store_Sessions.Parameters.AddWithValue("Img_Name", Session["Img_Name"]);

                          con.Open();
                          cmd\_Store\_Sessions.ExecuteNonQuery();
                          con.Close();
                  

                  but there is an error : The parameterized query '(@AdvKeyword nvarchar(2),@CampaignName nvarchar(2),@AdHeadLine n' expects the parameter '@Banner_Img', which was not supplied

                  T Offline
                  T Offline
                  T M Gray
                  wrote on last edited by
                  #8

                  I think that happens when the value is null. C# null and database null are different. If you want to insert a null into the database you need to use DBNull.Value.

                  L 1 Reply Last reply
                  0
                  • T T M Gray

                    I think that happens when the value is null. C# null and database null are different. If you want to insert a null into the database you need to use DBNull.Value.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    isn't that yet another reason for using SQLParameter? :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    L 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      isn't that yet another reason for using SQLParameter? :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      yes, it is.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups