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. urgent: can any one plz solve my problem regading SQL Query

urgent: can any one plz solve my problem regading SQL Query

Scheduled Pinned Locked Moved Database
databasecsharpasp-netsql-serversysadmin
13 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.
  • M Offline
    M Offline
    mavii
    wrote on last edited by
    #1

    i am developing an application in ASP.net,i am using the following sql query, but in SQL Server "criminal" table i want the same record to be inserted, so can anyone tell me how to merge the two queries in one query so that only one row is inserted strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')"; strQuery = "INSERT INTO criminal(pic_name) SELECT substring(cr_pic,11,30) FROM criminal";

    A Q C J P 5 Replies Last reply
    0
    • M mavii

      i am developing an application in ASP.net,i am using the following sql query, but in SQL Server "criminal" table i want the same record to be inserted, so can anyone tell me how to merge the two queries in one query so that only one row is inserted strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')"; strQuery = "INSERT INTO criminal(pic_name) SELECT substring(cr_pic,11,30) FROM criminal";

      A Offline
      A Offline
      arnoldpinto84
      wrote on last edited by
      #2

      Try this strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print,pic_name)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + substring(textBox6.Text,11,30)+"')";

      M 1 Reply Last reply
      0
      • M mavii

        i am developing an application in ASP.net,i am using the following sql query, but in SQL Server "criminal" table i want the same record to be inserted, so can anyone tell me how to merge the two queries in one query so that only one row is inserted strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')"; strQuery = "INSERT INTO criminal(pic_name) SELECT substring(cr_pic,11,30) FROM criminal";

        Q Offline
        Q Offline
        QPun
        wrote on last edited by
        #3

        Pardon me if i'm missunderstood, but those two queries is tend to insert different value to the table, so what is the reason you should merge them into one query?? maybe you could execute only one query by your application code , such as if xxx = "true" then strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')"; else strQuery = "INSERT INTO criminal(pic_name) SELECT substring(cr_pic,11,30) FROM criminal"; end if

        M 1 Reply Last reply
        0
        • A arnoldpinto84

          Try this strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print,pic_name)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + substring(textBox6.Text,11,30)+"')";

          M Offline
          M Offline
          mavii
          wrote on last edited by
          #4

          i had tried the same query but the following error comes Error 14 The name 'substring' does not exist in the current context C:\Documents and Settings\mehwish\My Documents\Visual Studio 2005\Projects\rescue15\new_wanted_criminal_record.cs 654 230 rescue15

          A 1 Reply Last reply
          0
          • Q QPun

            Pardon me if i'm missunderstood, but those two queries is tend to insert different value to the table, so what is the reason you should merge them into one query?? maybe you could execute only one query by your application code , such as if xxx = "true" then strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')"; else strQuery = "INSERT INTO criminal(pic_name) SELECT substring(cr_pic,11,30) FROM criminal"; end if

            M Offline
            M Offline
            mavii
            wrote on last edited by
            #5

            i cant apply if because i want to insert one record which includes the columns in both queries, actually i want some solution to insert cr_name,no_times,cr_desc,cr_pic,fing_print,pic_name using one query not two separate SQL quries i want one row to be inserted

            1 Reply Last reply
            0
            • M mavii

              i am developing an application in ASP.net,i am using the following sql query, but in SQL Server "criminal" table i want the same record to be inserted, so can anyone tell me how to merge the two queries in one query so that only one row is inserted strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')"; strQuery = "INSERT INTO criminal(pic_name) SELECT substring(cr_pic,11,30) FROM criminal";

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              1 - try reading the post on how to ask questions 2 - try using parameterised queries, so you're not open to injecton attacks 3 - try using real variable names, so your code is readable What was the question ? The most logical way I can see to do this, is to drop a column which always contains a substring of a value in another column, that's just duplicaton of data. Work out the pic name from the path in code, or write a proc that returns it as seperate data, but based entirely on the one column that contains all the data you need. OR, a yucky way to do it that would at least work, would be to store the Id of the column in question, and then update ony that, instead of doing an insert, which is obviously not what you want.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              1 Reply Last reply
              0
              • M mavii

                i had tried the same query but the following error comes Error 14 The name 'substring' does not exist in the current context C:\Documents and Settings\mehwish\My Documents\Visual Studio 2005\Projects\rescue15\new_wanted_criminal_record.cs 654 230 rescue15

                A Offline
                A Offline
                arnoldpinto84
                wrote on last edited by
                #7

                Check if this works out strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print,pic_name)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox6.Text.Substring(11,30) +"')";

                M 1 Reply Last reply
                0
                • A arnoldpinto84

                  Check if this works out strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print,pic_name)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox6.Text.Substring(11,30) +"')";

                  M Offline
                  M Offline
                  mavii
                  wrote on last edited by
                  #8

                  again error comes. Error is ArgumentOutOfrangeException was unhandled Index and length must refer to a location within the string. Parameter name: length what do i do:(

                  A P 2 Replies Last reply
                  0
                  • M mavii

                    again error comes. Error is ArgumentOutOfrangeException was unhandled Index and length must refer to a location within the string. Parameter name: length what do i do:(

                    A Offline
                    A Offline
                    arnoldpinto84
                    wrote on last edited by
                    #9

                    you are getting that error bcos the length that u are specifying is greater than the no of the characters in the string. make sure that you are specifying the right parameters in order to extract the name for the pic...try this strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print,pic_name)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox6.Text.Substring(11,textBox6.Text.Length-11) +"')"; i am assuming that the cr_pic name starts from the 11th position and u want it till the end. if you are still having a problem the post the format in which you are specifying the cr_pic

                    M 1 Reply Last reply
                    0
                    • M mavii

                      again error comes. Error is ArgumentOutOfrangeException was unhandled Index and length must refer to a location within the string. Parameter name: length what do i do:(

                      P Offline
                      P Offline
                      piyusgupta
                      wrote on last edited by
                      #10

                      ya mavii u have tried to fetch the char in the string at the position outside the string Use the "IndexOf" fun keeping in mind that it starts from 0 and end at Length-1. Regards _________ Piyus Gupta

                      1 Reply Last reply
                      0
                      • M mavii

                        i am developing an application in ASP.net,i am using the following sql query, but in SQL Server "criminal" table i want the same record to be inserted, so can anyone tell me how to merge the two queries in one query so that only one row is inserted strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')"; strQuery = "INSERT INTO criminal(pic_name) SELECT substring(cr_pic,11,30) FROM criminal";

                        J Offline
                        J Offline
                        joemonvarghese
                        wrote on last edited by
                        #11

                        STRQRY="INSERT INTO criminal SELECT '" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "' substring(cr_pic,11,30) FROM criminal "

                        1 Reply Last reply
                        0
                        • M mavii

                          i am developing an application in ASP.net,i am using the following sql query, but in SQL Server "criminal" table i want the same record to be inserted, so can anyone tell me how to merge the two queries in one query so that only one row is inserted strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')"; strQuery = "INSERT INTO criminal(pic_name) SELECT substring(cr_pic,11,30) FROM criminal";

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #12

                          Other people have already answered the questions for you but I feel the need to weigh in on your architecture a little bit. 1. You really shouldn't rely on the default names that you get in Visual Studio. Consider renaming textBox1, et al, to more meaningful names. 2. Don't create direct SQL strings like this. This is bad bad practice which should be avoided at all costs because you have opened your code up to SQL Injection attacks. See this[^] article for more information. 3. Consider moving your data access logic into a separate data access layer (DAL).

                          Deja View - the feeling that you've seen this post before.

                          1 Reply Last reply
                          0
                          • A arnoldpinto84

                            you are getting that error bcos the length that u are specifying is greater than the no of the characters in the string. make sure that you are specifying the right parameters in order to extract the name for the pic...try this strQuery = "INSERT INTO criminal(cr_name,no_times,cr_desc,cr_pic,fing_print,pic_name)VALUES('" + textBox1.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox6.Text.Substring(11,textBox6.Text.Length-11) +"')"; i am assuming that the cr_pic name starts from the 11th position and u want it till the end. if you are still having a problem the post the format in which you are specifying the cr_pic

                            M Offline
                            M Offline
                            mavii
                            wrote on last edited by
                            #13

                            thank you this is really working, can u suggest the solution for one more problem string strQuery = ""; openFileDialog1.ShowDialog(); string s = openFileDialog1.FileName; textBox6.Text = s; string st = "Pict" + Guid.NewGuid().ToString() + ".jpg"; File.Copy(openFileDialog1.FileName, Path.Combine(@"c:\inetpub\wwwroot\15 website\images", st)); strQuery = "INSERT INTO criminal(pic_name) VALUES(System.IO.Path.GetFileName(openFileDialog1.FileName))"; PROBLEM: error comes on this the SQL Query , am i inserting rightly?

                            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