need to insert name in the database using method
-
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(); } }
-
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(); } }
-
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# forumled mike
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(); } } } }
-
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(); } } } }
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. -
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(); } } } }
What is wrong with the above code? It compiles fine. Michael CP Blog [^] Development Blog [^]
-
What is wrong with the above code? It compiles fine. Michael CP Blog [^] Development Blog [^]
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.