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. Parametized queries in C#-Hard one.

Parametized queries in C#-Hard one.

Scheduled Pinned Locked Moved C#
questiondatabasecsharpsharepointjson
6 Posts 3 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.
  • F Offline
    F Offline
    falles01
    wrote on last edited by
    #1

    Hi there, I am using this stored procedure in sql. I have 6 tables. One is called employees. This is what I need to be able to do. A user enters a new employee into a winform, picks a role, division, manager, technicalskill set and applications from the drop down lists and hits save. The employee table should be the only one updated and has these columns only.( firstname, lastname, dvisionid, managerid, roleid,techskillsid, and appID). At the moment what is happening is its saving the firstname, lastname correctly, but the rest of the ID columns are null. It is updating the other tables with the string entered but what I need is the emplyee table to update with the corresponding ids. Is this alot more complicated then i thought? If I try to replace the role with roleid etc, it will just tell me I can't convert string to int which is understandable. How do I do this? CREATE PROCEDURE sp_InsertEmployee @Firstname nvarchar(50), @Lastname nvarchar(50), @Role nvarchar(50), @Manager nvarchar(50), @Division nvarchar(50) AS BEGIN SET NOCOUNT ON; INSERT INTO EMPLOYEES (FIRSTNAME, LASTNAME) VALUES (@FIRSTNAME, @LASTNAME) INSERT INTO [ROLE] ([ROLE]) VALUES (@ROLE) INSERT INTO MANAGER (MANAGER) VALUES (@MANAGER) INSERT INTO DIVISION:(((DIVISION) VALUES (@DIVISION) END GO:(( My C# code is like this: SqlCommand sqlC = new SqlCommand("sp_InsertEmployee", myConnection); sqlC.CommandType = CommandType.StoredProcedure; sqlC.Parameters.Add(new SqlParameter("@Firstname", SqlDbType.VarChar, 50, "Firstname")); sqlC.Parameters.Add(new SqlParameter("@Lastname", SqlDbType.VarChar, 50, "Lastname")); sqlC.Parameters.Add(new SqlParameter("@RoleID", SqlDbType.Int, 50, "RoleID")); sqlC.Parameters.Add(new SqlParameter("@ManagerID", SqlDbType.Int, 50, "ManagerID")); sqlC.Parameters.Add(new SqlParameter("@DivisionID", SqlDbType.Int, 50, "DivisionID")); //sqlC.Parameters[0].Value = 4; sqlC.Parameters[0].Value = FirstnameText.Text; sqlC.Parameters[1].Value = Lastnametext.Text; sqlC.Parameters[2].Value = RolecomboBoxTest.Text; sqlC.Parameters[3].Value = ManagercomboBox1.Text; sqlC.Parameters[4].Value = DivisioncomboBox2.Text; int i = sqlC.ExecuteNonQuery(); //sqlC.ExecuteNonQuery(); Sorry for pasting so much.

    C G 2 Replies Last reply
    0
    • F falles01

      Hi there, I am using this stored procedure in sql. I have 6 tables. One is called employees. This is what I need to be able to do. A user enters a new employee into a winform, picks a role, division, manager, technicalskill set and applications from the drop down lists and hits save. The employee table should be the only one updated and has these columns only.( firstname, lastname, dvisionid, managerid, roleid,techskillsid, and appID). At the moment what is happening is its saving the firstname, lastname correctly, but the rest of the ID columns are null. It is updating the other tables with the string entered but what I need is the emplyee table to update with the corresponding ids. Is this alot more complicated then i thought? If I try to replace the role with roleid etc, it will just tell me I can't convert string to int which is understandable. How do I do this? CREATE PROCEDURE sp_InsertEmployee @Firstname nvarchar(50), @Lastname nvarchar(50), @Role nvarchar(50), @Manager nvarchar(50), @Division nvarchar(50) AS BEGIN SET NOCOUNT ON; INSERT INTO EMPLOYEES (FIRSTNAME, LASTNAME) VALUES (@FIRSTNAME, @LASTNAME) INSERT INTO [ROLE] ([ROLE]) VALUES (@ROLE) INSERT INTO MANAGER (MANAGER) VALUES (@MANAGER) INSERT INTO DIVISION:(((DIVISION) VALUES (@DIVISION) END GO:(( My C# code is like this: SqlCommand sqlC = new SqlCommand("sp_InsertEmployee", myConnection); sqlC.CommandType = CommandType.StoredProcedure; sqlC.Parameters.Add(new SqlParameter("@Firstname", SqlDbType.VarChar, 50, "Firstname")); sqlC.Parameters.Add(new SqlParameter("@Lastname", SqlDbType.VarChar, 50, "Lastname")); sqlC.Parameters.Add(new SqlParameter("@RoleID", SqlDbType.Int, 50, "RoleID")); sqlC.Parameters.Add(new SqlParameter("@ManagerID", SqlDbType.Int, 50, "ManagerID")); sqlC.Parameters.Add(new SqlParameter("@DivisionID", SqlDbType.Int, 50, "DivisionID")); //sqlC.Parameters[0].Value = 4; sqlC.Parameters[0].Value = FirstnameText.Text; sqlC.Parameters[1].Value = Lastnametext.Text; sqlC.Parameters[2].Value = RolecomboBoxTest.Text; sqlC.Parameters[3].Value = ManagercomboBox1.Text; sqlC.Parameters[4].Value = DivisioncomboBox2.Text; int i = sqlC.ExecuteNonQuery(); //sqlC.ExecuteNonQuery(); Sorry for pasting so much.

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

      Actually, this is an easy question, about converting formats. int.TryParse is how you get an int out of a string.

      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
      • F falles01

        Hi there, I am using this stored procedure in sql. I have 6 tables. One is called employees. This is what I need to be able to do. A user enters a new employee into a winform, picks a role, division, manager, technicalskill set and applications from the drop down lists and hits save. The employee table should be the only one updated and has these columns only.( firstname, lastname, dvisionid, managerid, roleid,techskillsid, and appID). At the moment what is happening is its saving the firstname, lastname correctly, but the rest of the ID columns are null. It is updating the other tables with the string entered but what I need is the emplyee table to update with the corresponding ids. Is this alot more complicated then i thought? If I try to replace the role with roleid etc, it will just tell me I can't convert string to int which is understandable. How do I do this? CREATE PROCEDURE sp_InsertEmployee @Firstname nvarchar(50), @Lastname nvarchar(50), @Role nvarchar(50), @Manager nvarchar(50), @Division nvarchar(50) AS BEGIN SET NOCOUNT ON; INSERT INTO EMPLOYEES (FIRSTNAME, LASTNAME) VALUES (@FIRSTNAME, @LASTNAME) INSERT INTO [ROLE] ([ROLE]) VALUES (@ROLE) INSERT INTO MANAGER (MANAGER) VALUES (@MANAGER) INSERT INTO DIVISION:(((DIVISION) VALUES (@DIVISION) END GO:(( My C# code is like this: SqlCommand sqlC = new SqlCommand("sp_InsertEmployee", myConnection); sqlC.CommandType = CommandType.StoredProcedure; sqlC.Parameters.Add(new SqlParameter("@Firstname", SqlDbType.VarChar, 50, "Firstname")); sqlC.Parameters.Add(new SqlParameter("@Lastname", SqlDbType.VarChar, 50, "Lastname")); sqlC.Parameters.Add(new SqlParameter("@RoleID", SqlDbType.Int, 50, "RoleID")); sqlC.Parameters.Add(new SqlParameter("@ManagerID", SqlDbType.Int, 50, "ManagerID")); sqlC.Parameters.Add(new SqlParameter("@DivisionID", SqlDbType.Int, 50, "DivisionID")); //sqlC.Parameters[0].Value = 4; sqlC.Parameters[0].Value = FirstnameText.Text; sqlC.Parameters[1].Value = Lastnametext.Text; sqlC.Parameters[2].Value = RolecomboBoxTest.Text; sqlC.Parameters[3].Value = ManagercomboBox1.Text; sqlC.Parameters[4].Value = DivisioncomboBox2.Text; int i = sqlC.ExecuteNonQuery(); //sqlC.ExecuteNonQuery(); Sorry for pasting so much.

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        falles01 wrote:

        At the moment what is happening is its saving the firstname, lastname correctly, but the rest of the ID columns are null.

        Of course they are. You don't specify any values for them.

        falles01 wrote:

        It is updating the other tables with the string entered but what I need is the emplyee table to update with the corresponding ids. Is this alot more complicated then i thought?

        As you obviously thought that it would happen automatically, yes, it's a lot more complicated than that. You have to add the other values first, and get the id that is created for each record: declare @RoleId int insert into [ROLE] ([ROLE]) values (@ROLE) set @RoleId = scope_identity() Then you can use these values when you insert into EMPLOYEES.

        --- single minded; short sighted; long gone;

        F 1 Reply Last reply
        0
        • G Guffa

          falles01 wrote:

          At the moment what is happening is its saving the firstname, lastname correctly, but the rest of the ID columns are null.

          Of course they are. You don't specify any values for them.

          falles01 wrote:

          It is updating the other tables with the string entered but what I need is the emplyee table to update with the corresponding ids. Is this alot more complicated then i thought?

          As you obviously thought that it would happen automatically, yes, it's a lot more complicated than that. You have to add the other values first, and get the id that is created for each record: declare @RoleId int insert into [ROLE] ([ROLE]) values (@ROLE) set @RoleId = scope_identity() Then you can use these values when you insert into EMPLOYEES.

          --- single minded; short sighted; long gone;

          F Offline
          F Offline
          falles01
          wrote on last edited by
          #4

          Thank you, but I decided to just use my insert statement for now as that works. My question now is how exactly do I get selected values of a checkedlist box and store it as ID numbers in the database. I had something like this which is obviously wrong. It works for the comboboxes but then I heard you need for loops to check multiple selections in a checkedlistbox. Feel free to just change my code. if (this.techSkillsCheckListBox2.CheckedItems.Count > 0) { label1.Text = "worked"; :confused: foreach (string item in this.techSkillsCheckListBox2.CheckedItems) { string sql = "INSERT INTO employees(Firstname,Lastname,RoleID,DivisionID,ManagerID,TechnicalSkillsID) Values ('" + FirstnameText.Text.ToString() + "' , '" + Lastnametext.Text.ToString() + "' , " + RolecomboBoxTest.SelectedValue.ToString() + "," + DivisioncomboBox2.SelectedValue.ToString() + " , " + ManagercomboBox1.SelectedValue.ToString() + " , " + techSkillsCheckListBox2.SelectedValue.ToString() + ")"; Don't worry this won't be used commercially so I was told not to worry about injection attacks until I can get these correct. Thank you for helping out.

          G 1 Reply Last reply
          0
          • F falles01

            Thank you, but I decided to just use my insert statement for now as that works. My question now is how exactly do I get selected values of a checkedlist box and store it as ID numbers in the database. I had something like this which is obviously wrong. It works for the comboboxes but then I heard you need for loops to check multiple selections in a checkedlistbox. Feel free to just change my code. if (this.techSkillsCheckListBox2.CheckedItems.Count > 0) { label1.Text = "worked"; :confused: foreach (string item in this.techSkillsCheckListBox2.CheckedItems) { string sql = "INSERT INTO employees(Firstname,Lastname,RoleID,DivisionID,ManagerID,TechnicalSkillsID) Values ('" + FirstnameText.Text.ToString() + "' , '" + Lastnametext.Text.ToString() + "' , " + RolecomboBoxTest.SelectedValue.ToString() + "," + DivisioncomboBox2.SelectedValue.ToString() + " , " + ManagercomboBox1.SelectedValue.ToString() + " , " + techSkillsCheckListBox2.SelectedValue.ToString() + ")"; Don't worry this won't be used commercially so I was told not to worry about injection attacks until I can get these correct. Thank you for helping out.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            falles01 wrote:

            It works for the comboboxes but then I heard you need for loops to check multiple selections in a checkedlistbox.

            If you have a control that allows mutiple selections, then you have to use the SelectedItems property instead. You also have to change your database design so that it allows for multiple values.

            --- single minded; short sighted; long gone;

            F 1 Reply Last reply
            0
            • G Guffa

              falles01 wrote:

              It works for the comboboxes but then I heard you need for loops to check multiple selections in a checkedlistbox.

              If you have a control that allows mutiple selections, then you have to use the SelectedItems property instead. You also have to change your database design so that it allows for multiple values.

              --- single minded; short sighted; long gone;

              F Offline
              F Offline
              falles01
              wrote on last edited by
              #6

              It doesn't allow multiple selections though. Okay well apart from the database side of things I believe I have to use a 'for' loop. why does the following return an error?: The name 'Checked' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted. foreach (int indexChecked in techSkillsCheckListBox2.CheckedIndices) { string sql = "INSERT INTO employees(Firstname,Lastname,RoleID,DivisionID,ManagerID,TechnicalSkillsID) Values ('" + FirstnameText.Text.ToString() + "' , '" + Lastnametext.Text.ToString() + "' , " + RolecomboBoxTest.SelectedValue.ToString() + "," + DivisioncomboBox2.SelectedValue.ToString() + " , " + ManagercomboBox1.SelectedValue.ToString() + " , " + techSkillsCheckListBox2.GetItemCheckState(indexChecked).ToString() + ")"; SqlCommand sqlC = new SqlCommand(sql, myConnection); } } Anyone. I have been baffled by this for 8 hours. Its especially annoying when it was so easy to work out the combobox selections. Thanks again.

              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