having problem in retrieving primary key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shamaila
    New Member
    • Jul 2007
    • 29

    having problem in retrieving primary key

    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.ExecuteRea der();
    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(strQ uery1, conn);

    commm.ExecuteNo nQuery();


    but it is goiving an error on insert atement that
    "Use of unassigned local variable 'i' "
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shamaila
    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.ExecuteRea der();
    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(strQ uery1, conn);

    commm.ExecuteNo nQuery();


    but it is goiving an error on insert atement that
    "Use of unassigned local variable 'i' "
    Look at your code. You defined i inside the if block but you are trying to access it outside that if block.

    Comment

    • shamaila
      New Member
      • Jul 2007
      • 29

      #3
      Originally posted by r035198x
      Look at your code. You defined i inside the if block but you are trying to access it outside that if block.

      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??/

      Comment

      • dip_developer
        Recognized Expert Contributor
        • Aug 2006
        • 648

        #4
        Originally posted by shamaila
        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.ExecuteRea der();
        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(strQ uery1, conn);

        commm.ExecuteNo nQuery();


        but it is goiving an error on insert atement that
        "Use of unassigned local variable 'i' "
        you are declaring i globally....... like
        [CODE=css] int i;[/CODE]

        ok????......... .....
        now in the if block you are writing

        [CODE=css]
        if (dr.Read())
        {
        i = Convert.ToInt32 (dr.GetValue(1) );

        }
        [/CODE]

        then no problem will be there....please check your code.....
        whether you are again writing

        int i=Convert.ToInt 32(dr.GetValue( 1)); in the if block.....
        hope u understand..... ......

        Comment

        • shamaila
          New Member
          • Jul 2007
          • 29

          #5
          Originally posted by dip_developer
          you are declaring i globally....... like
          [CODE=css] int i;[/CODE]

          ok????......... .....
          now in the if block you are writing

          [CODE=css]
          if (dr.Read())
          {
          i = Convert.ToInt32 (dr.GetValue(1) );

          }
          [/CODE]

          then no problem will be there....please check your code.....
          whether you are again writing

          int i=Convert.ToInt 32(dr.GetValue( 1)); in the if block.....
          hope u understand..... ......

          yea i have again checked it ..i did it the same way but still getting the same error..

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by shamaila
            yea i have again checked it ..i did it the same way but still getting the same error..
            Can you post the code that you have now and the error message you are getting?

            Comment

            • shamaila
              New Member
              • Jul 2007
              • 29

              #7
              Originally posted by r035198x
              Can you post the code that you have now and the error message you are getting?


              [CODE=css] string strQuery = "";
              string strQuery1 = "";
              string id;
              int i;

              //******insert data into parent table**********

              strQuery = "INSERT INTO call_history(ph _num,c_date)VAL UES('" + textBox1.Text + "','" + textBox3.Text + "')";

              SqlCommand com = new SqlCommand(strQ uery, conn);
              conn.Open();
              com.ExecuteNonQ uery();


              //*** retrieve parent table's primary key *****

              id = "SELECT MAX(c_id) FROM call_history";
              SqlCommand com1 = new SqlCommand(id, conn);
              SqlDataReader dr = com1.ExecuteRea der();
              if (dr.Read())
              {
              i = Convert.ToInt32 (dr.GetValue(1) );

              }

              //************* insert data into child table

              strQuery1 = "INSERT INTO call_log(c_id,c _add)VALUES("+ i +",'"+textBox2. Text +"')";

              SqlCommand commm = new SqlCommand(strQ uery1, conn);

              commm.ExecuteNo nQuery();[/CODE]

              and the error msg is
              Use of unassigned local variable 'i'

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by shamaila
                [CODE=css] string strQuery = "";
                string strQuery1 = "";
                string id;
                int i;

                //******insert data into parent table**********

                strQuery = "INSERT INTO call_history(ph _num,c_date)VAL UES('" + textBox1.Text + "','" + textBox3.Text + "')";

                SqlCommand com = new SqlCommand(strQ uery, conn);
                conn.Open();
                com.ExecuteNonQ uery();


                //*** retrieve parent table's primary key *****

                id = "SELECT MAX(c_id) FROM call_history";
                SqlCommand com1 = new SqlCommand(id, conn);
                SqlDataReader dr = com1.ExecuteRea der();
                if (dr.Read())
                {
                i = Convert.ToInt32 (dr.GetValue(1) );

                }

                //************* insert data into child table

                strQuery1 = "INSERT INTO call_log(c_id,c _add)VALUES("+ i +",'"+textBox2. Text +"')";

                SqlCommand commm = new SqlCommand(strQ uery1, conn);

                commm.ExecuteNo nQuery();[/CODE]

                and the error msg is
                Use of unassigned local variable 'i'
                The if test may fail so the compiler needs to know that i is definitely assigned before being used in

                [CODE=css] strQuery1 = "INSERT INTO call_log(c_id,c _add)VALUES("+ i +",'"+textBox2. Text +"')";[/CODE]

                So give it a default value when you declare it like -1 for example.

                Comment

                • dip_developer
                  Recognized Expert Contributor
                  • Aug 2006
                  • 648

                  #9
                  Originally posted by r035198x
                  The if test may fail so the compiler needs to know that i is definitely assigned before being used in

                  [CODE=css] strQuery1 = "INSERT INTO call_log(c_id,c _add)VALUES("+ i +",'"+textBox2. Text +"')";[/CODE]

                  So give it a default value when you declare it like -1 for example.
                  exactly so........

                  Write
                  int i=0; when declaring it.....because your query may not return a value.......

                  Comment

                  • shamaila
                    New Member
                    • Jul 2007
                    • 29

                    #10
                    Originally posted by r035198x
                    The if test may fail so the compiler needs to know that i is definitely assigned before being used in

                    [CODE=css] strQuery1 = "INSERT INTO call_log(c_id,c _add)VALUES("+ i +",'"+textBox2. Text +"')";[/CODE]

                    So give it a default value when you declare it like -1 for example.

                    now i tried doing it without if statement
                    as simply writing;
                    i = Convert.ToInt32 (dr.GetValue(1) );


                    but following error occured
                    Invalid attempt to read when no data is present.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by shamaila
                      now i tried doing it without if statement
                      as simply writing;
                      i = Convert.ToInt32 (dr.GetValue(1) );


                      but following error occured
                      Invalid attempt to read when no data is present.
                      Why are you removing the if ?
                      Its job is to guard you against that error that you're getting now.

                      Comment

                      • shamaila
                        New Member
                        • Jul 2007
                        • 29

                        #12
                        Originally posted by r035198x
                        Why are you removing the if ?
                        Its job is to guard you against that error that you're getting now.
                        but is not working with that...

                        Comment

                        • radcaesar
                          Recognized Expert Contributor
                          • Sep 2006
                          • 759

                          #13
                          Declare the variable immediately after your class declaration.

                          Initialize it like int i=0 or Check for nullability when you cast the variable.

                          :)

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by shamaila
                            but is not working with that...
                            The if is not what's making it not work. Read* my reply above again.
                            When you declare i at the top there, initialize it to something and leave the if there.


                            *Reading: An extinct, ancient art which students used to obtain information.

                            Comment

                            • shamaila
                              New Member
                              • Jul 2007
                              • 29

                              #15
                              Originally posted by radcaesar
                              Declare the variable immediately after your class declaration.

                              Initialize it like int i=0 or Check for nullability when you cast the variable.

                              :)
                              i tried it... there was no error on debugging but when i tried inserting values and clicked on save button the following exception was handled
                              Index was outside the bounds of the array.

                              Comment

                              Working...