Results 1 to 4 of 4

Thread: [Solved] Use of unassigned local variable "dr"

  1. #1

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    Resolved [Solved] Use of unassigned local variable "dr"

    Why is she (C#) treating "dr" as an unassigned local variable? Or am I missing something?
    Code:
                natureofcollections n=new natureofcollections();
                SqlDataReader dr;
                try
                {
                    monkey.sql.sqlconnection.openconnection(cn);
                    dr=new monkey.sql.sqlcommand("selectallnatureofcollections",cn).executereader();
                    while(dr.Read())
                    {
                        n.add(new natureofcollection(dr[0].ToString(),
                            dr[1].ToString(),
                            dr[2].ToString()));
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    dr.Close();
                    monkey.sql.sqlconnection.closeconnection(cn);
                }
                return n;
    Thank you.
    Last edited by brown monkey; Nov 16th, 2004 at 04:12 AM.

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    if monkey.sql.sqlconnection.openconnection(cn); fails then dr will never get initialised try
    Code:
    SqlDataReader dr = null;

  3. #3

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    Kinda strict, huh... I thank you.

    Once again, the day has been save... Thanks to the DeadEyes

    Last edited by brown monkey; Nov 16th, 2004 at 04:15 AM.

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    no problem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width