So I am trying to write data to a SQL directory the code is suppose to take a finger print and store it in a directory and for some reason the data in the DataRow myRow is always null I have tried everything.

                sqlCommandText = "select * from xluser where xlkeyid = '" + keyId + "'";

                sqlCommand.CommandText = sqlCommandText;
                SqlDataReader sqlDataReader;
                sqlDataReader = sqlCommand.ExecuteReader();
                if (!sqlDataReader.HasRows)
                    Logging.LogMsg("RequestManager::HandleWriteFPData: " + clientRequest.Queue + "  Failed to save    fingerprint data for key id " + keyId, false);
                Logging.LogMsg("RequestManager::HandleWriteFPData: " + clientRequest.Queue + "  Fingerprint data saved for key id " + keyId, false);
                sqlDataReader.Close();

                SqlDataAdapter da = new SqlDataAdapter(sqlCommandText, sqlConnection);
                MyCB = new SqlCommandBuilder(da);
                DataSet ds = new DataSet("FPDATA");
                da.Fill(ds, "FPDATA");


                DataRow myRow;
                // dmm::111907::if no rows then exception is thrown which will cancel the transaction
                // which is the preferred action at this point (do not check row count)
                // if ( ds.Tables["FPDATA"].Rows.Count > 0 ) 
                myRow = ds.Tables["FPDATA"].Rows[0];

The reason I know its null is because this check returns true

if (myRow["FPDATA"] != System.DBNull.Value) 

Edited 4 Years Ago by kspecks

This means the select query did not return any row. There is no data in the table regarding the condition (in where clause).

I know the sqlDataReader.HasRows returns true because it skips that log and procides to the second log. Why would there not be any data in the table. Sorry I am a C++ trained programmer this SQL thing is brand new to me.

This can be appropriatelly done like:

        //int keyId = 1; //this is your variable, and only my example (erase it from here)
        DataTable table = new DataTable();
        using (SqlConnection conn = new SqlConnection("connString"))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(@"SELECT * FROM xluser WHERE xlkeyid = @param1", conn))
            {
                da.SelectCommand.Parameters.Add("@param1", SqlDbType.Int).Value = keyId;
                da.Fill(table);
            }
        }
        if (table.Rows.Count > 0)
        {
            //table has data...
            //if you want to get a particular row:
            DataRow row1 = table.Rows[0]; //row1 is sure NOT null, it sure HAS data!
        }
        else
        {
            //show message of there is no data inside table
        }

I will try that and let you know on Monday when I get back to the office. Thanks for the help so far.

This article has been dead for over six months. Start a new discussion instead.