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. need help with SQL

need help with SQL

Scheduled Pinned Locked Moved Database
helpdatabasecsharpsql-serversysadmin
6 Posts 5 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.
  • S Offline
    S Offline
    Sujay chakraborty
    wrote on last edited by
    #1

    hello, this is a sql statement that i m trying to use in a Window application in .NET. query: <pre>string selCmd = "SELECT eInfo.EmpID, eInfo.Name, lInfo.LeaveEnteredOn, lInfo.LeaveFrom, lInfo.LeaveTo, lInfo.LeaveType" + "FROM lmsEmpInfo AS eInfo INNER JOIN lmsLeaveInfo AS lInfo ON lInfo.EmpID = eInfo.EmpID" + "WHERE (lInfo.LeaveGranted = 0)";</pre> the problem is this query works fine when i m running it on the SQL server management studio, but gives an error of "Incorrect syntax near AS", when i try to execute it with the following code: <code> SqlDataAdapter myAd = new SqlDataAdapter(); SqlConnection myCon = new SqlConnection(); DataSet myDs = new DataSet(); myCon.ConnectionString = Properties.Settings.Default.LeaveMgmtConnectionString.ToString(); myAd.SelectCommand = new SqlCommand(selCmd, myCon); myAd.Fill(myDs, "Recent_Leaves");</code> Please help me to rectify this mistake......... I would also like to have suggestions on How can i change the entire row collection of a DataGridView with the SelectedIndexChange event of a combobox and that too according to query that is being fired at the same event any help or assistance is greatly appreciated!! regards

    W P 2 Replies Last reply
    0
    • S Sujay chakraborty

      hello, this is a sql statement that i m trying to use in a Window application in .NET. query: <pre>string selCmd = "SELECT eInfo.EmpID, eInfo.Name, lInfo.LeaveEnteredOn, lInfo.LeaveFrom, lInfo.LeaveTo, lInfo.LeaveType" + "FROM lmsEmpInfo AS eInfo INNER JOIN lmsLeaveInfo AS lInfo ON lInfo.EmpID = eInfo.EmpID" + "WHERE (lInfo.LeaveGranted = 0)";</pre> the problem is this query works fine when i m running it on the SQL server management studio, but gives an error of "Incorrect syntax near AS", when i try to execute it with the following code: <code> SqlDataAdapter myAd = new SqlDataAdapter(); SqlConnection myCon = new SqlConnection(); DataSet myDs = new DataSet(); myCon.ConnectionString = Properties.Settings.Default.LeaveMgmtConnectionString.ToString(); myAd.SelectCommand = new SqlCommand(selCmd, myCon); myAd.Fill(myDs, "Recent_Leaves");</code> Please help me to rectify this mistake......... I would also like to have suggestions on How can i change the entire row collection of a DataGridView with the SelectedIndexChange event of a combobox and that too according to query that is being fired at the same event any help or assistance is greatly appreciated!! regards

      W Offline
      W Offline
      www Developerof NET
      wrote on last edited by
      #2

      Sujay chakraborty wrote:

      string selCmd = "SELECT eInfo.EmpID, eInfo.Name, lInfo.LeaveEnteredOn, lInfo.LeaveFrom, lInfo.LeaveTo, lInfo.LeaveType" + "FROM lmsEmpInfo AS eInfo INNER JOIN lmsLeaveInfo AS lInfo ON lInfo.EmpID = eInfo.EmpID" + "WHERE (lInfo.LeaveGranted = 0)";

      First of all there should be some space in the string where u concat the 3 strings... and for ur other query pls clear ur question a bit more...

      When you fail to plan, you are planning to fail.

      S 1 Reply Last reply
      0
      • S Sujay chakraborty

        hello, this is a sql statement that i m trying to use in a Window application in .NET. query: <pre>string selCmd = "SELECT eInfo.EmpID, eInfo.Name, lInfo.LeaveEnteredOn, lInfo.LeaveFrom, lInfo.LeaveTo, lInfo.LeaveType" + "FROM lmsEmpInfo AS eInfo INNER JOIN lmsLeaveInfo AS lInfo ON lInfo.EmpID = eInfo.EmpID" + "WHERE (lInfo.LeaveGranted = 0)";</pre> the problem is this query works fine when i m running it on the SQL server management studio, but gives an error of "Incorrect syntax near AS", when i try to execute it with the following code: <code> SqlDataAdapter myAd = new SqlDataAdapter(); SqlConnection myCon = new SqlConnection(); DataSet myDs = new DataSet(); myCon.ConnectionString = Properties.Settings.Default.LeaveMgmtConnectionString.ToString(); myAd.SelectCommand = new SqlCommand(selCmd, myCon); myAd.Fill(myDs, "Recent_Leaves");</code> Please help me to rectify this mistake......... I would also like to have suggestions on How can i change the entire row collection of a DataGridView with the SelectedIndexChange event of a combobox and that too according to query that is being fired at the same event any help or assistance is greatly appreciated!! regards

        P Offline
        P Offline
        Paul Conrad
        wrote on last edited by
        #3

        Your code is subject to sql injection attacks with the string concatenation. Look up Colin Mackay's article on this site about preventing such attacks.

        "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

        K 1 Reply Last reply
        0
        • P Paul Conrad

          Your code is subject to sql injection attacks with the string concatenation. Look up Colin Mackay's article on this site about preventing such attacks.

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

          K Offline
          K Offline
          Kanniah
          wrote on last edited by
          #4

          "SELECT eInfo.EmpID, eInfo.Name, lInfo.LeaveEnteredOn, lInfo.LeaveFrom, lInfo.LeaveTo, lInfo.LeaveType " + " FROM lmsEmpInfo AS eInfo INNER JOIN lmsLeaveInfo AS lInfo ON lInfo.EmpID = eInfo.EmpID " + " WHERE (lInfo.LeaveGranted = 0)" When you try to form an Sql by Concating always insert one space before '"' Hope this helps Kan

          P 1 Reply Last reply
          0
          • W www Developerof NET

            Sujay chakraborty wrote:

            string selCmd = "SELECT eInfo.EmpID, eInfo.Name, lInfo.LeaveEnteredOn, lInfo.LeaveFrom, lInfo.LeaveTo, lInfo.LeaveType" + "FROM lmsEmpInfo AS eInfo INNER JOIN lmsLeaveInfo AS lInfo ON lInfo.EmpID = eInfo.EmpID" + "WHERE (lInfo.LeaveGranted = 0)";

            First of all there should be some space in the string where u concat the 3 strings... and for ur other query pls clear ur question a bit more...

            When you fail to plan, you are planning to fail.

            S Offline
            S Offline
            Sujay chakraborty
            wrote on last edited by
            #5

            well first of all thanx for helping me out with such silly mistakes. I really appreciate that. well as far as my second doubt is concerned, what i m trying to do is to fetch records from the database to a DataGridView according to the query passed(i.e. a select command which will fill up the table in the dataset), what i m doing is m not externally attaching this DataGridView with the concerned table (from the properties of the datagridview), rather after filling up the dataset and the table m doing this in the SelectedIndexChanged event of the ComboBox: DataGridView1.DataSource = null; DataGridView1.DataMember = null; DataGridView1.Refresh(); myAD.Fill(ds,"tbl1"); //myAD is DataAdapter and ds is the DataSet DataGridView1.DataSource = ds; DataGridView1.DataMember = "tbl1"; DataGridView1.Refresh(); well this code works fine for the first time and fetches me the exact result, but when i change the item in the combobox it still keeps the previous previous recods and adds the new ones below. I want the previous records to disappear.I tried the method DataGridView1.Rows.Clear() but its giving me error. Please do explain me what is actually happening wrong because m new to this technology, your help is greatly awaited!!! :) Regards Sujay

            1 Reply Last reply
            0
            • K Kanniah

              "SELECT eInfo.EmpID, eInfo.Name, lInfo.LeaveEnteredOn, lInfo.LeaveFrom, lInfo.LeaveTo, lInfo.LeaveType " + " FROM lmsEmpInfo AS eInfo INNER JOIN lmsLeaveInfo AS lInfo ON lInfo.EmpID = eInfo.EmpID " + " WHERE (lInfo.LeaveGranted = 0)" When you try to form an Sql by Concating always insert one space before '"' Hope this helps Kan

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

              Kanniah wrote:

              When you try to form an Sql by Concating always insert one space before '"'

              Are you really suggesting that this prevents a SQL Injection Attack? Please tell me that you are kidding, and that you know that this is absolute rubbish.

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

              My blog | My articles

              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