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. need to insert name in the database using method

need to insert name in the database using method

Scheduled Pinned Locked Moved C#
databasesysadminsecurityhelp
6 Posts 4 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
    Mamphekgo
    wrote on last edited by
    #1

    hi.i am struggling to insert name in the database using method.it gives me this error "Use of unassigned local variable 'empname'" and my code is as follows. public void InsertValues(string empname1) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "integrated security=SSPI;initial catalog=Employee;server = za211149;persist security info=False"; conn.Open(); try { SqlCommand cmdInsert = new SqlCommand(); cmdInsert.Connection = conn; empname = empname1; cmdInsert.CommandText = "INSERT INTO jakes VALUES(@empname)"; cmdInsert.Parameters.Add(new SqlParameter("@empname",System.Data.SqlDbType.VarChar,50,"empname")); cmdInsert.Parameters["@empname"].Value = empname.ToString(); if (cmdInsert.ExecuteNonQuery() !=0) { MessageBox.Show("Record inserted"); } else { MessageBox.Show("record not inserted"); } //return true; } catch(Exception E) { MessageBox.Show(E.ToString()); //return false; } finally { conn.Close(); } }

    L 1 Reply Last reply
    0
    • M Mamphekgo

      hi.i am struggling to insert name in the database using method.it gives me this error "Use of unassigned local variable 'empname'" and my code is as follows. public void InsertValues(string empname1) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "integrated security=SSPI;initial catalog=Employee;server = za211149;persist security info=False"; conn.Open(); try { SqlCommand cmdInsert = new SqlCommand(); cmdInsert.Connection = conn; empname = empname1; cmdInsert.CommandText = "INSERT INTO jakes VALUES(@empname)"; cmdInsert.Parameters.Add(new SqlParameter("@empname",System.Data.SqlDbType.VarChar,50,"empname")); cmdInsert.Parameters["@empname"].Value = empname.ToString(); if (cmdInsert.ExecuteNonQuery() !=0) { MessageBox.Show("Record inserted"); } else { MessageBox.Show("record not inserted"); } //return true; } catch(Exception E) { MessageBox.Show(E.ToString()); //return false; } finally { conn.Close(); } }

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      The code you posted does not contain a declaration for the variable empname

      "Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?"
      Colin Angus Mackay in the C# forum

      led mike

      M 1 Reply Last reply
      0
      • L led mike

        The code you posted does not contain a declaration for the variable empname

        "Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?"
        Colin Angus Mackay in the C# forum

        led mike

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

        actually my code is like this. using System; using System.Data.SqlClient; using System.Data.SqlTypes; using System.Windows.Forms; namespace bb { /// /// Summary description for connection. /// public class connection1 { public string empname; public connection1() { // // TODO: Add constructor logic here // } public void InsertValues(string empname1) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "integrated security=SSPI;initial catalog=Employee;server = za211149;persist security info=False"; conn.Open(); try { SqlCommand cmdInsert = new SqlCommand(); cmdInsert.Connection = conn; empname = empname1; cmdInsert.CommandText = "INSERT INTO jakes VALUES(@empname)"; cmdInsert.Parameters.Add(new SqlParameter("@empname",System.Data.SqlDbType.VarChar,50,"empname")); cmdInsert.Parameters["@empname"].Value = empname.ToString(); if (cmdInsert.ExecuteNonQuery() !=0) { MessageBox.Show("Record inserted"); } else { MessageBox.Show("record not inserted"); } //return true; } catch(Exception E) { MessageBox.Show(E.ToString()); //return false; } finally { conn.Close(); } } } }

        M M 2 Replies Last reply
        0
        • M Mamphekgo

          actually my code is like this. using System; using System.Data.SqlClient; using System.Data.SqlTypes; using System.Windows.Forms; namespace bb { /// /// Summary description for connection. /// public class connection1 { public string empname; public connection1() { // // TODO: Add constructor logic here // } public void InsertValues(string empname1) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "integrated security=SSPI;initial catalog=Employee;server = za211149;persist security info=False"; conn.Open(); try { SqlCommand cmdInsert = new SqlCommand(); cmdInsert.Connection = conn; empname = empname1; cmdInsert.CommandText = "INSERT INTO jakes VALUES(@empname)"; cmdInsert.Parameters.Add(new SqlParameter("@empname",System.Data.SqlDbType.VarChar,50,"empname")); cmdInsert.Parameters["@empname"].Value = empname.ToString(); if (cmdInsert.ExecuteNonQuery() !=0) { MessageBox.Show("Record inserted"); } else { MessageBox.Show("record not inserted"); } //return true; } catch(Exception E) { MessageBox.Show(E.ToString()); //return false; } finally { conn.Close(); } } } }

          M Offline
          M Offline
          Mairaaj Khan
          wrote on last edited by
          #4

          Mamphekgo wrote:

          cmdInsert.Parameters.Add(new SqlParameter("@empname",System.Data.SqlDbType.VarChar,50,"empname")); cmdInsert.Parameters["@empname"].Value = empname.ToString();

          If I'm not wrong string type doesn't need conversion to string. Since it's already a string. Initializing the variable empname with empty string might solve your problem. Best of Luck. _____________________________ Success is not something to wait for, its something to work for.

          1 Reply Last reply
          0
          • M Mamphekgo

            actually my code is like this. using System; using System.Data.SqlClient; using System.Data.SqlTypes; using System.Windows.Forms; namespace bb { /// /// Summary description for connection. /// public class connection1 { public string empname; public connection1() { // // TODO: Add constructor logic here // } public void InsertValues(string empname1) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "integrated security=SSPI;initial catalog=Employee;server = za211149;persist security info=False"; conn.Open(); try { SqlCommand cmdInsert = new SqlCommand(); cmdInsert.Connection = conn; empname = empname1; cmdInsert.CommandText = "INSERT INTO jakes VALUES(@empname)"; cmdInsert.Parameters.Add(new SqlParameter("@empname",System.Data.SqlDbType.VarChar,50,"empname")); cmdInsert.Parameters["@empname"].Value = empname.ToString(); if (cmdInsert.ExecuteNonQuery() !=0) { MessageBox.Show("Record inserted"); } else { MessageBox.Show("record not inserted"); } //return true; } catch(Exception E) { MessageBox.Show(E.ToString()); //return false; } finally { conn.Close(); } } } }

            M Offline
            M Offline
            Michael P Butler
            wrote on last edited by
            #5

            What is wrong with the above code? It compiles fine. Michael CP Blog [^] Development Blog [^]

            M 1 Reply Last reply
            0
            • M Michael P Butler

              What is wrong with the above code? It compiles fine. Michael CP Blog [^] Development Blog [^]

              M Offline
              M Offline
              Mairaaj Khan
              wrote on last edited by
              #6

              Michael P Butler wrote:

              What is wrong with the above code?

              Nothing! But does it make sense to convert into its own type? or do we have need for it? _____________________________ Success is not something to wait for, its something to work for.

              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