having problem in retrieving primary key from parent table
-
i want to retrieve primary key of parent table and then send this key to child table,as they have one-one relationship i've written code string id = "SELECT MAX(c_id) FROM call_history"; SqlCommand com1 = new SqlCommand(id, conn); SqlDataReader dr = com1.ExecuteReader(); if (dr.Read()) { int i = Convert.ToInt32(dr.GetValue(1)); } strQuery1 = "INSERT INTO call_log(c_id,c_add)VALUES("+ i +",'"+textBox2.Text +"')"; SqlCommand commm = new SqlCommand(strQuery1, conn); commm.ExecuteNonQuery(); but it is goiving an error on insert atement that "Use of unassigned local variable 'i' "
$h@ma!|@
-
i want to retrieve primary key of parent table and then send this key to child table,as they have one-one relationship i've written code string id = "SELECT MAX(c_id) FROM call_history"; SqlCommand com1 = new SqlCommand(id, conn); SqlDataReader dr = com1.ExecuteReader(); if (dr.Read()) { int i = Convert.ToInt32(dr.GetValue(1)); } strQuery1 = "INSERT INTO call_log(c_id,c_add)VALUES("+ i +",'"+textBox2.Text +"')"; SqlCommand commm = new SqlCommand(strQuery1, conn); commm.ExecuteNonQuery(); but it is goiving an error on insert atement that "Use of unassigned local variable 'i' "
$h@ma!|@
$h@ma!|@ wrote:
int i = Convert.ToInt32(dr.GetValue(1));
Your variable is defined inside your brackets, but you try to use it after it's dropped out of scope. Move your } to the bottom of this code block and it will work. I'd recommend reading a book on C#, if you're unaware of hte scoping rules.
$h@ma!|@ wrote:
strQuery1 = "INSERT INTO call_log(c_id,c_add)VALUES("+ i +",'"+textBox2.Text +"')";
I recommend you do some reading on SQL injection attacks. This sort of code allows people to run any SQL they like on your database.
$h@ma!|@ wrote:
$h@ma!|@
Does that translate to shemale ?
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 )
-
$h@ma!|@ wrote:
int i = Convert.ToInt32(dr.GetValue(1));
Your variable is defined inside your brackets, but you try to use it after it's dropped out of scope. Move your } to the bottom of this code block and it will work. I'd recommend reading a book on C#, if you're unaware of hte scoping rules.
$h@ma!|@ wrote:
strQuery1 = "INSERT INTO call_log(c_id,c_add)VALUES("+ i +",'"+textBox2.Text +"')";
I recommend you do some reading on SQL injection attacks. This sort of code allows people to run any SQL they like on your database.
$h@ma!|@ wrote:
$h@ma!|@
Does that translate to shemale ?
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 )