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. General Programming
  3. C#
  4. Execution of query??

Execution of query??

Scheduled Pinned Locked Moved C#
databasehelpquestion
8 Posts 7 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.
  • U Offline
    U Offline
    User 10532513
    wrote on last edited by
    #1

    try { string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,Guardian's Name,Contact.No,Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian's Name,@Contact.No,@Address,@Email-Address)"; SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand(s,con); cmd.Connection = con; cmd.Parameters.AddWithValue("@Name", textBox1.Text); cmd.Parameters.AddWithValue("@Age", textBox2.Text); cmd.Parameters.AddWithValue("@Class", textBox3.Text); cmd.Parameters.AddWithValue("@Gender", textBox4.Text); cmd.Parameters.AddWithValue("@Guardian's Name", textBox5.Text); cmd.Parameters.AddWithValue("@Contact.No", textBox6.Text); cmd.Parameters.AddWithValue("@Address", textBox7.Text); cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Saved!"); } My query can't executed successfully please help ??:confused:

    B Richard DeemingR L T D 6 Replies Last reply
    0
    • U User 10532513

      try { string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,Guardian's Name,Contact.No,Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian's Name,@Contact.No,@Address,@Email-Address)"; SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand(s,con); cmd.Connection = con; cmd.Parameters.AddWithValue("@Name", textBox1.Text); cmd.Parameters.AddWithValue("@Age", textBox2.Text); cmd.Parameters.AddWithValue("@Class", textBox3.Text); cmd.Parameters.AddWithValue("@Gender", textBox4.Text); cmd.Parameters.AddWithValue("@Guardian's Name", textBox5.Text); cmd.Parameters.AddWithValue("@Contact.No", textBox6.Text); cmd.Parameters.AddWithValue("@Address", textBox7.Text); cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Saved!"); } My query can't executed successfully please help ??:confused:

      B Offline
      B Offline
      BBatts
      wrote on last edited by
      #2

      You can't have the "'" in Guardian's Name. What database are you using?

      1 Reply Last reply
      0
      • U User 10532513

        try { string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,Guardian's Name,Contact.No,Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian's Name,@Contact.No,@Address,@Email-Address)"; SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand(s,con); cmd.Connection = con; cmd.Parameters.AddWithValue("@Name", textBox1.Text); cmd.Parameters.AddWithValue("@Age", textBox2.Text); cmd.Parameters.AddWithValue("@Class", textBox3.Text); cmd.Parameters.AddWithValue("@Gender", textBox4.Text); cmd.Parameters.AddWithValue("@Guardian's Name", textBox5.Text); cmd.Parameters.AddWithValue("@Contact.No", textBox6.Text); cmd.Parameters.AddWithValue("@Address", textBox7.Text); cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Saved!"); } My query can't executed successfully please help ??:confused:

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Member 10564850 wrote:

        My query can't executed successfully please help ??

        As Griff mentioned on Saturday:

        OriginalGriff wrote:[^]

        Just remember in future - the better the info you give us, the quicker we can give you a solution!

        Once again, you've posted your code, but not the error message, which leaves us to play another game of "guess the error". Assuming the problem is with the column names containing special characters, the quick fix is to wrap the affected column names in square brackets. You'll also need to fix the parameter names:

        string s = "INSERT INTO [Students Records] (Name, Age, Class, Gender, [Guardian's Name], [Contact.No], Address, [Email-Address]) VALUES(@Name, @Age, @Class, @Gender, @GuardiansName, @ContactNo, @Address, @EmailAddress)";

        A better solution is to change your database structure so that the table and column names do not contain anything other than letters and numbers - no spaces, no punctuation, and no extended characters. The same restrictions apply to parameter names. If that doesn't solve the problem, then you'll need to provide the details of the exception being thrown when you try to execute the query.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        D 1 Reply Last reply
        0
        • U User 10532513

          try { string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,Guardian's Name,Contact.No,Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian's Name,@Contact.No,@Address,@Email-Address)"; SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand(s,con); cmd.Connection = con; cmd.Parameters.AddWithValue("@Name", textBox1.Text); cmd.Parameters.AddWithValue("@Age", textBox2.Text); cmd.Parameters.AddWithValue("@Class", textBox3.Text); cmd.Parameters.AddWithValue("@Gender", textBox4.Text); cmd.Parameters.AddWithValue("@Guardian's Name", textBox5.Text); cmd.Parameters.AddWithValue("@Contact.No", textBox6.Text); cmd.Parameters.AddWithValue("@Address", textBox7.Text); cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Saved!"); } My query can't executed successfully please help ??:confused:

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

          Answered here[^]. ..and now you know "why" it is not done.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          • U User 10532513

            try { string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,Guardian's Name,Contact.No,Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian's Name,@Contact.No,@Address,@Email-Address)"; SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand(s,con); cmd.Connection = con; cmd.Parameters.AddWithValue("@Name", textBox1.Text); cmd.Parameters.AddWithValue("@Age", textBox2.Text); cmd.Parameters.AddWithValue("@Class", textBox3.Text); cmd.Parameters.AddWithValue("@Gender", textBox4.Text); cmd.Parameters.AddWithValue("@Guardian's Name", textBox5.Text); cmd.Parameters.AddWithValue("@Contact.No", textBox6.Text); cmd.Parameters.AddWithValue("@Address", textBox7.Text); cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Saved!"); } My query can't executed successfully please help ??:confused:

            T Offline
            T Offline
            thatraja
            wrote on last edited by
            #5

            Don't repost[^]

            thatraja

            Code converters | Education Needed No thanks, I am all stocked up. - Luc Pattyn When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is - Henry Minute

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Member 10564850 wrote:

              My query can't executed successfully please help ??

              As Griff mentioned on Saturday:

              OriginalGriff wrote:[^]

              Just remember in future - the better the info you give us, the quicker we can give you a solution!

              Once again, you've posted your code, but not the error message, which leaves us to play another game of "guess the error". Assuming the problem is with the column names containing special characters, the quick fix is to wrap the affected column names in square brackets. You'll also need to fix the parameter names:

              string s = "INSERT INTO [Students Records] (Name, Age, Class, Gender, [Guardian's Name], [Contact.No], Address, [Email-Address]) VALUES(@Name, @Age, @Class, @Gender, @GuardiansName, @ContactNo, @Address, @EmailAddress)";

              A better solution is to change your database structure so that the table and column names do not contain anything other than letters and numbers - no spaces, no punctuation, and no extended characters. The same restrictions apply to parameter names. If that doesn't solve the problem, then you'll need to provide the details of the exception being thrown when you try to execute the query.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Whoops! Already moved. Sorry! :-\

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              1 Reply Last reply
              0
              • U User 10532513

                try { string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,Guardian's Name,Contact.No,Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian's Name,@Contact.No,@Address,@Email-Address)"; SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand(s,con); cmd.Connection = con; cmd.Parameters.AddWithValue("@Name", textBox1.Text); cmd.Parameters.AddWithValue("@Age", textBox2.Text); cmd.Parameters.AddWithValue("@Class", textBox3.Text); cmd.Parameters.AddWithValue("@Gender", textBox4.Text); cmd.Parameters.AddWithValue("@Guardian's Name", textBox5.Text); cmd.Parameters.AddWithValue("@Contact.No", textBox6.Text); cmd.Parameters.AddWithValue("@Address", textBox7.Text); cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Saved!"); } My query can't executed successfully please help ??:confused:

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Again, you're putting spaces and special characters in the parameter names. DO NOT USE SPACES AND SPECIAL CHARACTERS IN TABLE NAMES, FIELD NAMES AND PARAMETER NAMES! Your field names should be something like this:

                Name, Age, Class, Gender, GuardianName, ContactPhone, Address, EmailAddress

                By making the code more "eyeball friendly" to read, you are giving yourself headaches debugging the resulting code. "Eyeball friendly" is NOT "compiler friendly"!

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                1 Reply Last reply
                0
                • U User 10532513

                  try { string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,Guardian's Name,Contact.No,Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian's Name,@Contact.No,@Address,@Email-Address)"; SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand(s,con); cmd.Connection = con; cmd.Parameters.AddWithValue("@Name", textBox1.Text); cmd.Parameters.AddWithValue("@Age", textBox2.Text); cmd.Parameters.AddWithValue("@Class", textBox3.Text); cmd.Parameters.AddWithValue("@Gender", textBox4.Text); cmd.Parameters.AddWithValue("@Guardian's Name", textBox5.Text); cmd.Parameters.AddWithValue("@Contact.No", textBox6.Text); cmd.Parameters.AddWithValue("@Address", textBox7.Text); cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Saved!"); } My query can't executed successfully please help ??:confused:

                  M Offline
                  M Offline
                  Marco Bertschi
                  wrote on last edited by
                  #8

                  Clean-up crew needed, grammar spill... - Nagy Vilmos

                  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