Avatar of Adnan
Adnan
Flag for Norway asked on

DBNull Exceptin: The value for column 'WinAut' in table 'Operator' is DBNull.

hi iam getting this exception when im on line:    chkWinAuthOperator.Checked = drUser.WinAut;....

Can somone se what iam doin wrong here?=
private void SetUserDetails()
        {
            Int32 intUserId = 0;
            if (gridViewUsers.Rows.Count > 0)
            {
                if (gridViewUsers.SelectedRows.Count == 0)
                    intUserId = Convert.ToInt32(gridViewUsers.Rows[0].Cells["Operator_ID"].Value);
                else
                    intUserId = Convert.ToInt32(gridViewUsers.SelectedRows[0].Cells["Operator_ID"].Value);
 
                DsOperatorAccess.OperatorRow drUser = dsOperatorAccess1.Operator.FindByOperator_ID(intUserId);
 
                TmpOperator_Id = intUserId;
 
                txtUserName.Text = drUser.UserName;
                txtFirstName.Text = drUser.FirstName;
                txtLastName.Text = drUser.LastName;
                txtEmail.Text = drUser.Email;
                txtPassword.Text = "";
                chkAdmin.Checked = Convert.ToBoolean(drUser.Admin);
                chkInUse.Checked = Convert.ToBoolean(drUser.InUse);
                chkWinAuthOperator.Checked = drUser.WinAut;    // Getting error HERE
                lstLanguage.SelectedIndex = lstLanguage.FindStringExact(drUser.Language);
                BindOperatorAccess(drUser);
            }
        }

Open in new window

C#.NET Programming

Avatar of undefined
Last Comment
Adnan

8/22/2022 - Mon
sunithnair

Try this one
private void SetUserDetails()
        {
            Int32 intUserId = 0;
            if (gridViewUsers.Rows.Count > 0)
            {
                if (gridViewUsers.SelectedRows.Count == 0)
                    intUserId = Convert.ToInt32(gridViewUsers.Rows[0].Cells["Operator_ID"].Value);
                else
                    intUserId = Convert.ToInt32(gridViewUsers.SelectedRows[0].Cells["Operator_ID"].Value);
 
                DsOperatorAccess.OperatorRow drUser = dsOperatorAccess1.Operator.FindByOperator_ID(intUserId);
 
                TmpOperator_Id = intUserId;
 
                txtUserName.Text = drUser.UserName;
                txtFirstName.Text = drUser.FirstName;
                txtLastName.Text = drUser.LastName;
                txtEmail.Text = drUser.Email;
                txtPassword.Text = "";
                chkAdmin.Checked = Convert.ToBoolean(drUser.Admin);
                chkInUse.Checked = Convert.ToBoolean(drUser.InUse);
                if(drUser.WinAut  != DBNull.Value)
                chkWinAuthOperator.Checked = drUser.WinAut;    // Getting error HERE
                lstLanguage.SelectedIndex = lstLanguage.FindStringExact(drUser.Language);
                BindOperatorAccess(drUser);
            }
        }

Open in new window

Adnan

ASKER
hmm oki...iam getting error on if statement: Error      16      Operator '!=' cannot be applied to operands of type 'bool' and 'System.DBNull'......????!


if(drUser.WinAut  != DBNull.Value)
                chkWinAuthOperator.Checked = drUser.WinAut;  
sunithnair

Sorry try this way
if(!drUser.WinAut.Equals(DBNull.Value))

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
Adnan

ASKER
Still getting the same error message. strange.... :(
sunithnair

Which error message is it the 1st error or the 2nd error?
Adnan

ASKER
The value for column 'WinAuth' in table 'Operator' is DBNull.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
hdkelly

Suni's solution is almost there.

You are using a strongly typed data row which is useful in most cases but it does mean you can't compare its values with DBNull. However, you should be able to get at the underlying objects with the indexer on your row:

You may need to add some logic to be able to handle an unexpected null value in your data.
if(drUser["WinAut"] != DBNull.Value)
                chkWinAuthOperator.Checked = drUser.WinAut;  

Open in new window

ASKER CERTIFIED SOLUTION
sunithnair

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
HarryNS


From dataReader you can use, but you cannot give the column name here, you have to use column ordinal.
If (oReader.IsDbnull(0))
//Your code.
Adnan

ASKER
oki, i thinnk it is some problom with my Datasat: DsOperatorAccess.OperatorRow drUser = dsOperatorAccess1.Operator.FindByOperator_ID(intUserId);

When i debug and check the columns in my datarow drUser i get this message on the column WinAuth = 'drUser.WinAuth' threw an exception of type 'System.Data.StrongTypingException'

and InnerException = {"Specified cast is not valid."}


 :(
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Adnan

ASKER
sunithnair: i get the samme error message even i try you if statements as you showed in you replays...
Adnan

ASKER
Sorry guys, i had created the column wrong in my Sql operator table, the value of the colmn was null, so i set the value to 1 and it worked fine