vendredi 14 décembre 2018

What is the correct way to check if a cell in a database is null

I have an employee attendance project. this is my table:

CREATE TABLE [dbo].[tbl_attendanceSheet] (
    [Id]           INT            IDENTITY (1, 1) NOT NULL,
    [memberCode]   NVARCHAR (20)  NULL,
    [name]         NVARCHAR (20)  NULL,
    [date]         NVARCHAR (20)  NULL,
    [clockin]      NVARCHAR (20)  NULL,
    [clockout]     NVARCHAR (20)  NULL,
    [delay]        NVARCHAR (20)  NULL,
    [HouresWorked] NVARCHAR (20)  NULL,
    [desc]         NVARCHAR (150) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

I want to manage for the employees whenever they enter their code in the textbox the program record their enter or leave time.

for some reson I have to check if some cells are null.

private void textBoxX1_KeyDown(object sender, KeyEventArgs e)
{
    sqlcon.Close();
    sqlcon.Open();

    if (e.KeyCode == Keys.Enter)
    {
        string t = lbl_Time.Text;
        string d = lbl_Date.Text;

        string selectQueryName = "SELECT name FROM tbl_attendanceMembers where memberCode=" + "'" + textBoxX1.Text + "'";
        var sqlcmdName = new SqlCommand(selectQueryName, sqlcon);
        var resultName = sqlcmdName.ExecuteScalar();

        string selectQueryId = "SELECT MAX(id) FROM tbl_attendanceSheet";
        var sqlcmdId = new SqlCommand(selectQueryId, sqlcon);
        var resultId = sqlcmdId.ExecuteScalar();

        (1)if (resultId != null)
        {
            string selectQueryCockin = "SELECT Clockin FROM tbl_attendanceSheet where id=" + "resultId";
            var sqlcmdCockin = new SqlCommand(selectQueryCockin, sqlcon);
            var resultCockin = sqlcmdId.ExecuteScalar();

           (2)if (resultCockin != null)
            {
                (3)if (resultName != null)
                {

                    this.lbl_mmbrname.Text = resultName.ToString();
                    this.lbl_timestored.Text = t;
                    textBoxX1.Clear();                       
                }    
            }
        }
        else //if result id == null
        {
            sqlcon.Open();
            SqlCommand sqlcmdClockin = new SqlCommand("InputClockIn", sqlcon);
            sqlcmdClockin.CommandType = CommandType.StoredProcedure;
            sqlcmdClockin.Parameters.AddWithValue("@InputDate", d);
            sqlcmdClockin.Parameters.AddWithValue("@InputTime", t);
            sqlcmdClockin.ExecuteNonQuery();
            SqlDataAdapter sqlda = new SqlDataAdapter("SELECT * FROM tbl_attendanceMembers", sqlcon);
            DataTable dt = new DataTable();
            sqlda.Fill(dt);
            dataGridView1.DataSource = dt;
        }

        sqlcon.Close();

    }
}

Whenever the equality signs in those points are "==", it jumps to the else part. How ever whenever the equality signs in those points are "!=", it goes through all of the ifs. this phenomenon is regardless of nullity of the reletive cells in the database. what am I doint wrong?

I have checked program via break poits several times... I have checked different situations which reletive cells are null and are not null. I dont know what is wrong.

By the first sqlcon.close(); is because if I dont write it it will say connection is not closed which doesn't make sense to me because I have written a sqlcon.close(); down there...

Aucun commentaire:

Enregistrer un commentaire