0

I was wondering if someone could help me out with this issue.

Basically I have a Web App, that searches a DB for a person based on an UID. After it finds the name, I open another db conn and search for their Managers email address.

However i'm getting the "Object reference not set to an instance" error, which i'm assuming something is null and it doesn't like it? that correct.

Here is my code.

public partial class Leaver : System.Web.UI.Page
{


        string Managers_Name = null;
        string Managers_Email = null;


protected void Page_Load(object sender, EventArgs e)
{

}

    protected void Button1_SearchDB(object sender, EventArgs e)
{
    SqlDataReader reader = null;
    SqlConnection conn = null;

    try
    {
        conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["App_NewStarterConnectionString"].ConnectionString);

        {
            conn.Open();
            using (SqlCommand cmd =
                new SqlCommand("SELECT * FROM dbo.tb_starters WHERE Payrol = @Payrol", conn))
            {
                cmd.Parameters.AddWithValue("@Payrol", Payrol.Value);
                reader = cmd.ExecuteReader();



                while (reader.Read())
                {
                   Fname.Value = reader["FirstName"].ToString();
                   Lname.Value = reader["LastName"].ToString();
                   Payrol.Value = reader["Payrol"].ToString();
                   section.Value = reader["Section"].ToString();
                   Managers_Name = reader["Manager"].ToString();


                }

            }

        }

    }
    catch (Exception ee)
    {
        throw ee;
    }
    finally {

        GetManagersEmail();

        if (reader != null)
            reader.Close();

        if (conn.State == ConnectionState.Open)
            conn.Close();
    }

}

protected void GetManagersEmail()
{
    SqlDataReader reader_new = null;
    SqlConnection conn_new = null;

    try
    {
        conn_new = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["App_NewStarterConnectionString"].ConnectionString);
        {
            conn_new.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT Email FROM dbo.tb_starters WHERE FullName = @ManagersName", conn_new))
            {
                cmd.Parameters.AddWithValue("@ManagersName", Managers_Name);
                while (reader_new.Read())
                {
                    Managers_Email = reader_new["Email"].ToString();
                    Response.Write(Managers_Email);
                }
            }

        }

    }
    catch (Exception ee)
    {
        throw ee;
    }
    finally
    {

        if (reader_new != null)
            reader_new.Close();

        if (conn_new.State == ConnectionState.Open)
            conn_new.Close();

    }
}
3
  • 1
    The stack trace would be quite helpfull Commented Mar 15, 2013 at 11:18
  • Try using Convert.ToString() instead of .ToString() Commented Mar 15, 2013 at 11:18
  • Your reader is null in your get managers method Commented Mar 15, 2013 at 11:24

3 Answers 3

1

Did you get the reference for reader_new as you did in reader = cmd.ExecuteReader();

try with

reader_new = cmd.ExecuteReader();
Sign up to request clarification or add additional context in comments.

Comments

0

However i'm getting the "Object reference not set to an instance" error, which i'm assuming something is null and it doesn't like it? that correct.

Correct. Performing an action on a NULL object throws that error.

Without the stack trace, this is a guess.

Possibly, your error is around here:

Fname.Value = reader["FirstName"].ToString();
Lname.Value = reader["LastName"].ToString();
Payrol.Value = reader["Payrol"].ToString();
section.Value = reader["Section"].ToString();
Managers_Name = reader["Manager"].ToString();

If one of these values is NULL, an Object reference not set to an instance would be thrown.

1 Comment

Thanks for you help.. "Manager" was not set correctly in the database. So when it tried to pull data back based on that it threw the error.. I guess I need to put a catch in there.. in case its null.
0

Don't assign null value to your SqlDataReader and SqlConnection at the begining just define them like

   SqlDataReader reader;
    SqlConnection conn ;

This might be the problem !!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.