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. Web Development
  3. ASP.NET
  4. having problem in retrieving primary key from parent table

having problem in retrieving primary key from parent table

Scheduled Pinned Locked Moved ASP.NET
help
3 Posts 2 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.
  • H Offline
    H Offline
    h ma
    wrote on last edited by
    #1

    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!|@

    C 1 Reply Last reply
    0
    • H 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!|@

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

      $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 1 Reply Last reply
      0
      • C Christian Graus

        $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 Offline
        H Offline
        h ma
        wrote on last edited by
        #3

        this time i defined i globaly but still having the same problem...is there any problem in the insert statement?? have i written the sql command rite??/

        $h@ma!|@

        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