Problem running eventhandler

2

I have a class, which is executed under a button, it makes two select in a bank. I am trying to do this with EventHandler , however, when running the system, it executes, but gives the error in the line: OnDataChange(info);

  

Error Object reference not set to an instance of an object

Followthecodebelow:

publicvoidconsulta(){stringsqltring=@"Data Source=tptspo01sql01.database.windows.net;Initial Catalog=Tptecnologia;Integrated Security=False;User ID=********Password=*********@;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False";
        string _sql = string.Empty;
        SqlConnection sqlcon = new SqlConnection(sqltring);

        string usu, pass;
        int eid, uid;
        usu = textBox1.Text;
        pass = textBox2.Text;

        sqlcon.Open();

            _sql = "SELECT Id_empresa FROM Login WHERE usuario = @usuario AND Passwd = @Passwd";

            SqlCommand cmd = new SqlCommand(_sql, sqlcon);

            cmd.Parameters.Add("@usuario", SqlDbType.VarChar).Value = usu;
            cmd.Parameters.Add("@Passwd", SqlDbType.VarChar).Value = pass;

            eid = (int)cmd.ExecuteScalar();

            _sql = "SELECT Id_usu FROM Login WHERE usuario = @usuario AND Passwd = @Passwd";

            SqlCommand cmd1 = new SqlCommand(_sql, sqlcon);

            cmd1.Parameters.Add("@usuario", SqlDbType.VarChar).Value = usu;
            cmd1.Parameters.Add("@Passwd", SqlDbType.VarChar).Value = pass;                

            uid = (int)cmd1.ExecuteScalar();

        sqlcon.Close();

        Hashtable info = new Hashtable();
        info.Add("eid1", Convert.ToString(eid));
        info.Add("uid1", Convert.ToString(uid));
        OnDataChange(info);

        Tinicial frm2 = new Tinicial();

        frm2.Show();

    }
    
asked by anonymous 05.06.2016 / 01:29

1 answer

4

First problem, do not use HashTable , this class is obsolete and problematic. Prefer Dictionary<string, string> , or who knows Dictionary<string, int> . Are you sure you need this? It sounds like a gambiarra, but I may be wrong. With integer values you can use eid.ToString() , it's simpler.

I have not seen other problems in the specific location. Are you using other things not recommended?

In the line mentioned, you can not give an error, because even if the error were inside the OnDataChange method, it would be because info would be null, which is not the case. What could happen is that this info could not be a HashTable with this data. It probably happens somewhere else.

The code has other links that could be much better, but I will limit myself to the reported.

    
05.06.2016 / 02:27