user2121793
user2121793

Reputation: 31

Unable to cast object of type 'System.Data.DataSet

I got this error after running the debbugger to see the error:

"Unable to cast object of type 'System.Data.DataSet' to type 'NLHosp.DataSet"

"NLHosp" is the name of my database.

here is the code:

  private void btnLogin_Click(object sender, System.EventArgs e)
    {
        string strUser;
        string strPass;
        string sMsg = "";

        strUser = txtUserID.Text ;
        strPass = txtPassword.Text ;

        DataSet o_Find = new DataSet ();
        Users oUsers = new Users();

        try
        {
            o_Find = (DataSet)oUsers.FindData(strUser,strPass);
            sMsg = "Welcome " + o_Find.Tables ["Login"].Rows[0]["UserName"].ToString ();

            switch (strUser)
            {
                case "Admissions":
                    frmAdmissions admitForm = new frmAdmissions ();
                    admitForm.Visible = true;
                    admitForm.Activate();
                    break;
                case "Admin":
                case "Nurse":
                case "Doctor":
                    frmMenu menuForm = new frmMenu ();
                    menuForm.oCurrent.UserName = strUser;
                    menuForm.Visible = true;
                    menuForm.Activate();
                    menuForm.SelectUser();
                    break;
            }

        }

Upvotes: 0

Views: 2071

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500275

It looks like you've declared your own type called DataSet within the NLHosp namespace, and that's what you're trying to cast to - but the FindData is just returning a System.Data.DataSet object.

To start with, I'd strongly encourage you to rename NLHosp.DataSet so that it doesn't clash with existing system type names. Then you should consider what you actually wanted to cast it to. If you really meant to cast it to your custom type (and work), then you need to look at FindData and work out why it's only returning a System.Data.DataSet. It looks like you only need things from System.Data.DataSet though...

Upvotes: 2

Related Questions