Problem running eventhandler

Asked

Viewed 70 times

2

I have a class, which is executed under a button, it makes two select in a bank. apos, need to take these two data and make available to use in a second form, I’m trying to make EventHandler, however, when running the system, it executes, but gives the error on the line: OnDataChange(info);

Error Object reference not set for an object instance

SS

Follows code:

 public void consulta()
    {
        string sqltring = @"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();

    }
  • In which line does the error occur? The number I saw, I need indication of what it is, since there is no.

  • Good night friend, after the line : info. Add("uid1", Convert.Tostring(uid));, where I call Ondatachange(info). in this line.

  • I can’t see a mistake there. I see a beautiful gambiarra, but it should work. At least this mistake, in this place, should not happen.

1 answer

4


First problem: don’t 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 looks like a scam, but I could be wrong. With integer values you can use eid.ToString(), is simpler.

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

In the mentioned line there is no way to give an error, because even if the error was within the method OnDataChange, would because of the info be null, which is not the case. What can happen is that this info could not be a HashTable with this data. Probably happens elsewhere.

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

  • Friend, thanks for the comments and I will try to use this Dictionary. I’m starting to venture in c#, so sorry for the mistakes. Actually, I’m trying to make "uid" and "Eid" available in form2, I would have some suggestions?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.