Find the null value of a Datatable - c#

I am building a AWS lambda function .net core.
The issue I am encountering is, when there is no data in the row / column of the datatable I still get a count of one, in turn getData != null && getData.Count() > 0 defaults true and then throws a NullRefrenceError since the value is null when it goes to the loop, I have tried checking for multiple null types in datatable with them still defaulting to true.
Is there another way to check for nullable values, to avoid the assignment causing the error in a datatable column / row.
public object emailGets ( AType invoiceNum, ILambdaContext context )
{
Connection conn = new Connection();
try
{
string query = "SELECT QUERY";
conn.getData(query);
DataRow[] getData = conn.Dt.Select();
if(getData != null && getData.Count() > 0)
{
foreach (var item in getData)
{
string yourItem = item.Field<String>("email").ToString();
}
return new userData { email = yourItem};
}
else
{
return new userEmailAddress { email = null};
}
} catch ( Exception e )
{
throw e;
}
}
}
public class userEmailAddress
{
public string email { get; set; }
}

ToString() will throw a NullReferenceException when the source is null. So when you do
string yourItem = item.Field<String>("email").ToString();
and the item.Field<String>("email") part returns null, you'll get that exception.
Luckily, that ToString() call is redundant, so you can simply remove it and just have:
string yourItem = item.Field<String>("email");
Keep in mind that yourItem can now be null here.

Related

Null Reference Exception when trying to store an array

So I'm trying to store fields from a text file into an array. But when I reach the line
data = record.Trim().Split('*');
I get an error saying "Object reference not set to an instance of an object."
public bool matchCustomer(string accountID, string record)
{
String[] data = new String[5];
data = null;
data = record.Trim().Split('*');
this.accountNumber = data[0];
if (accountID == this.accountNumber)
{
return true;
}
else
{
return false;
}
}
Here is where the method is called:
public bool findCustomer(string accountNumber)
{
string record = Global.currentFile.getNextRecord(ref endOfFile);
bool okay = Global.customer.matchCustomer(accountNumber, record);
return okay;
}
Here is getNextRecord:
public string getNextRecord(ref Boolean endOfFileFlag)
{
string nextRecord;
endOfFileFlag = false;
nextRecord = reader.ReadLine();
if (nextRecord == null)
{
endOfFileFlag = true;
}
else
{
recordReadCount += 1;
} // end if
return (nextRecord);
} // end getNextRecord
First, you can simplify your code by replacing:
String[] data = new String[5];
data = null;
data = record.Trim().Split('*');
with just a single line:
string[] data = record.Trim().Split('*');
This is a correct statement, because you don't know the max index (elements) of the string[] array returning by Split() function.
Second, make sure that record!=null, and also it has a string containing "*" characters used as delimiter in Split() function.
Let's look at this line of code
data = record.Trim().Split('*');
If a NullReferenceException occurred here, that means you must be calling a method on a null object. In this case, the only possible object is record.
Now we know what is null, but why is it null? Let's look at this method:
public bool findCustomer(string accountNumber)
{
string record = Global.currentFile.getNextRecord(ref endOfFile);
bool okay = Global.customer.matchCustomer(accountNumber, record);
return okay;
}
Apparently you are using the return value of getNextRecord to call matchCustomer. This means that getNextRecord must return be returning null! So let's find out why getNextRecord returns null:
public string getNextRecord(ref Boolean endOfFileFlag)
{
string nextRecord;
endOfFileFlag = false;
nextRecord = reader.ReadLine();
if (nextRecord == null)
{
endOfFileFlag = true;
}
else
{
recordReadCount += 1;
} // end if
return (nextRecord);
} // end getNextRecord
If the method return nextRecord, that means nextRecord is null. And how did you get nextRecord? reader.ReadLine!
So the ultimate reason why record is null is that reader.ReadLine is null.
To avoid this exception, you need to first check whether record is null, then call the method on it:
if (record != null) {
data = record.Trim().Split('*');
} else {
// do other stuff
}
In C# 6, this can be simplified to
data = record?.Trim().Split('*');
If record is null, data will be null too!
Also, note that this code is redundant:
String[] data = new String[5];
data = null;
You are creating a bunch of strings and then setting the array to null. What's the point? So you can just remove that, and change the next line to:
string[] data = record?.Trim().Split('*');

C# and Reflection don't work twice in a row

I've a problem and I can't figured it out how to solve it.
I've a class for fetching data from a Database, in this class I've a method for a simple select * this method is called
List<T> All<T>(string tableName)
and you have to specify which resource you want to fetch, for example
All<User>("users")
And, aside from the classic SQL Reader and SQL Command, the core of the method is this
public override List<T> All<T>(string resource)
{
List<T> result = new List<T>();
using (MySqlConnection sqlConnection = new MySqlConnection(connectionString))
{
sqlConnection.Open();
try
{
string query = "SELECT * FROM " + resource + " WHERE 1=1";
using (MySqlCommand sqlCommand = new MySqlCommand(query, sqlConnection))
{
lock (locker)
{
MySqlDataReader reader = sqlCommand.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
T model = Activator.CreateInstance<T>();
Dictionary<string, object> _properties = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
string property = reader.GetName(i);
object value = reader.GetValue(i);
_properties.Add(property, value);
}
var type = model.GetType();
var method = type.GetMethod("SetProperties");
var invoked = method.Invoke(model, new object[] { _properties });
result.Add(model);
}
}
reader.Close();
}
}
}
catch (Exception ex)
{
Program.eventLogger.Add(new Event(EventType.Error, "SQL Data Providers", "Exception catched on All", ex));
}
finally
{
sqlConnection.Close();
}
}
return result;
}
Basically, based on the Type from the method header, the method will try to create an new instance of the specific type, later for each field from the query, it will fills all the attributes of the class on a temporaneous list. Once it's done it will try to call the method "SetProperties" which basically set every attributes of the class using reflection.
This is the core of SetProperties, equal for each entity:
public virtual bool SetProperties(Dictionary<string,object> properties)
{
if(this.ValidateData(properties))
{
FillNullableAttributes(properties);
// Iterate trough every key : value pairs in properties
foreach (KeyValuePair<string, object> kvp in properties)
{
if (this.data.Contains(kvp.Key))
{
var property = this.GetType().GetProperty(kvp.Key);
PropertyInfo propertyInfo = this.GetType().GetProperty(kvp.Key);
// Set the current fetched key with the given value if !null
if (kvp.Value != null)
{
Type fetchedType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
object safeConversion = (kvp.Value == null || kvp.Value == DBNull.Value) ? null : Convert.ChangeType(kvp.Value, fetchedType);
if (propertyInfo.CanWrite)
{
propertyInfo.SetValue(this, safeConversion, null);
}
}
}
}
return true;
}
return false;
}
In conclusion the result, which is a list, will be returned and the specific Entity will have its own BindingList filled. The binding list, for each entity is described as follow:
public static BindingList<Seller> items = new BindingList<Seller>();
This code works fine, even if there's a lot of space for improvements I know, but if I called it twice like this:
User.items = new BindingList<User>(provider.All<User>("users"));
User.items = new BindingList<User>(provider.All<User>("users"));
The second list will be filled by empty entities, the counting of the will be correct but they will be empties... and that shouldn't occurs.
The only thing that I figured it out, from the debugging, is that on the second call
var invoked = method.Invoke(model, new object[] { _properties });
invoked is set to false.
The result of:
var invoked = method.Invoke(model, new object[] { _properties });
Is the return value from your SetProperties method, not whether the method was invoked as your question indicates. Your SetProperties method is telling you that it was unable to do its work, debug that and you will find your answer.

Failed to enable constraints using TableAdapters

I'm trying to check if a username exists in my table when everytime a character is entered in the TextBox. Here is my code:
Within the register.aspx.cs file I have a TextChanged event on the TextBox:
protected void username_txt_TextChanged(object sender, EventArgs e)
{
string check = authentication.checkUsername(username_txt.Text);
if(check == "false")
{
username_lbl.Text = "Available";
}
else
{
username_lbl.Text = "Not Available";
}
}
It calls this method:
public static string checkUsername(string Username)
{
userInfoTableAdapters.usersTableAdapter userInfoTableAdapters = new userInfoTableAdapters.usersTableAdapter();
DataTable userDataTable = userInfoTableAdapters.checkUsername(Username);
DataRow row = userDataTable.Rows[0];
int rowValue = System.Convert.ToInt16(row["Users"]);
if (rowValue == 0)
{
return "false";
}
else
{
return "true";
}
}
The query that is being executed is:
SELECT COUNT(username) AS Users FROM users WHERE (username = #Username)
For some reason, it keeps breaking on this line:
DataTable userDataTable = userInfoTableAdapters.checkUsername(Username);
It gives an error that says:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
Just incase, the username field in my table is Unique and Not Null, I have tried just executing the query itself and it works perfectly so it isn't at the query end.
Does anyone understand what I am doing wrong?
Your query doesn't return the row - so using a TableAdapter query that returns the DataTable is inappropriate in this case.
I'd recommend using your query with something like the function below. I took the liberty of actually returning boolean....
public static bool checkUsername(string userName)
{
SqlClient.SqlCommand withCmd = new SqlClient.SqlCommand();
bool result = false;
withCmd.Connection.Open();
withCmd.CommandType = CommandType.text;
withCmd.CommandText = "SELECT COUNT(username) AS Users FROM users WHERE (username = #Username)"
withCmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("#Username", System.Data.SqlDbType.VarChar, 16)).Value = userName;
try {
int intResult;
object scalarResult = withCmd.ExecuteScalar();
if ((scalarResult != DBNull.Value)
&& (scalarResult != null)
&& (int.TryParse(scalarResult, out intResult)))
result = (intResult==1);
} catch (Exception ex) {
result = false; // hmm, bad...can't tell handle error...
} finally {
// only close if we opened the connection above ...
withCmd.Connection.Close();
}
}
return result;
}
A TableAdapter does support scalar queries on the Table Object, when you add and name your query, check the properties of that query and be sure its ExecuteMode is Scalar. It will then return the integer value, not the row!
On the other hand, if you want to keep your structure, change the query to actually return the row, something like
SELECT uu.* AS dbo.Users uu FROM users WHERE (username = #Username)
and make the result of the checkUsername() function depend on the number of rows returned (which should be 1 or zero....)

How to fix a boolean method that returns true everytime asp.net

I designed my webpage to read a data string then display the results on labels in an html table. I am attempting to highlight the row that my database reads as a current order. My only problem is only one record is set to be active but they all highlight as if they were active. I use an array to set my data and I also use the label to get the ID I need (all is in code below). I have posted my method and where I use it in the asp page load. How can I fix my method to return correctly?
The implementing of the method in page load
if (lineData.IsCurrentOrderFind(L68.Text))
{
myTable.Rows[1].Cells[0].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[1].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[2].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[3].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[4].BgColor = "#FE2E2E";
}
Here is method that label above gets passed to
public bool IsCurrentOrderFind(string itemNumber)
{
StringBuilder sqlString = new StringBuilder();
sqlString.Append("SELECT * ");
sqlString.Append("FROM WorkOrder ");
sqlString.Append("WHERE LineNumber = " + ConfigurationManager.AppSettings["Line"] + " AND LineCompleted = 0 AND (ScaleGroup LIKE '%1' OR ScaleGroup LIKE '%3') ");
sqlString.Append(" AND CaseGenNum6 = #CaseGenNum6");
SqlDataReader reader = null;
SqlConnection dbConn = App_Code.DBHelper.getConnection();
SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("#CaseGenNum6", itemNumber) };
try
{
reader = App_Code.DBHelper.executeQuery(dbConn, sqlString.ToString(), parameters);
while (reader.Read())
{
IsCurrentOrder = (reader["IsCurrentOrder"] != DBNull.Value && !string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString())) ? true : false;
}
reader.Close();
reader.Dispose();
dbConn.Close();
dbConn.Dispose();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (dbConn != null)
{
try { dbConn.Close(); dbConn.Dispose(); }
catch { }
}
if (reader != null)
{
try { reader.Close(); reader.Dispose(); }
catch { }
}
}
if (IsCurrentOrder == true) I realize this is not necessary
{
return true;
}
else
{
return false;
}
}
The problem could be with this expression:
!string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString())
Instead of calling ToString(), try simply casting it to a string:
!string.IsNullOrEmpty((string)reader["IsCurrentOrder"])
Possibly even better (the previous line might throw an exception if it's not really a string):
!string.IsNullOrEmpty(reader["IsCurrentOrder"] as string)
The reason being is that if the string is really null, calling ToString() will return a non-null string "null".
IsCurrentOrder is not declared locally. It seems to be declared at a higher scope. When you enter this function, nothing is initializing the variable (back to false). So, it is remaining at its last setting. Try this code instead:
public bool IsCurrentOrderFind(string itemNumber)
{
bool IsCurrentOrder = false;
//and the rest of your source code
the line
IsCurrentOrder = (reader["IsCurrentOrder"] != DBNull.Value && !string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString())) ? true : false;
}
It's not actually checking the value of the field, only that it's not null or empty.
Try
if(
(reader["IsCurrentOrder"] != DBNull.Value
&&
!string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString()))
)
{
IsCurrentOrder = reader["IsCurrentOrder"];
}
else
IsCurrentOrder = false;
I think there is a lot of refactoring you could do to this method though that will simplify the logic.

Invalid Cast DateTime?

I have a class with the following code
public cCase(string pCaseNo, string pMode)
{
if (pMode == "new")
{
this._caseNo = Validate_CaseNo(pCaseNo);
}
if (pMode == "existing")
{
try
{
int intValidatedCaseNo = Validate_CaseNo(pCaseNo);
string sqlText = "SELECT * FROM tblCases WHERE CaseNo = #CaseNo;";
string strConnection = cConnectionString.BuildConnectionString();
SqlConnection linkToDB = new SqlConnection(strConnection);
linkToDB.Open();
SqlCommand sqlCom = new SqlCommand(sqlText, linkToDB);
sqlCom.Parameters.Add("#CaseNo", SqlDbType.Int);
sqlCom.Parameters["#CaseNo"].Value = intValidatedCaseNo;
SqlDataReader caseReader = sqlCom.ExecuteReader();
if (caseReader.HasRows)
while (caseReader.Read())
{
this._claimant = caseReader["Claimant"].ToString();
this._defendant = caseReader["Defendant"].ToString();
this._caseType = caseReader["CaseType"].ToString();
this._occupation = caseReader["Occupation"].ToString();
this._doa = (DateTime?)caseReader["DOA"];
this._dateClosed = (DateTime?)caseReader["DateClosed"];
this._dateSettled = (DateTime?)caseReader["DateSettled"];
this._dateInstructed = (DateTime?)caseReader["DateInstructed"];
this._status = caseReader["Status"].ToString();
this._instructionType = caseReader["InstructionType"].ToString();
this._feeEstimate = (decimal?)caseReader["FeeEstimate"];
this._amountClaimed = (decimal?)caseReader["AmountClaimed"];
this._amountSettled = (decimal?)caseReader["AmountSettled"];
this._caseManager = caseReader["CaseManager"].ToString();
}
caseReader.Close();
linkToDB.Close();
linkToDB.Dispose();
}
catch (Exception eX)
{
throw new Exception("Error finding case" + Environment.NewLine + eX.Message);
}
}
}
However the Datetime? casts fail with an 'Invalid Cast'.
I've checked the SQL database and the field is storing valid dates
So I cant work out why, as I extract info via the DataReader into my app, the datetime fields are causing an Invalid Cast.
Please help.
You're going to want to change the line that reads:
this._doa = (DateTime?)caseReader["DOA"];
to:
if (caseReader["DOA"] != DBNull.Value)
this._doa.Value = (DateTime)caseReader["DOA"];
As well as all similar lines.
DBNull values cannot be casted from Nullable types.
Your DateTime fields probably hold a DBNull value which you cannot convert directly.
However, I'd use an extension method on your DataReader for convinience.
public static class DataReaderExtensions
{
public static DateTime? ReadNullableDateTime(this IDataReader reader, string column)
{
return reader.IsDBNull(column) ? (DateTime?)null : reader.GetDateTime(column);
}
}
// Usage
this._dateInstructed = CaseReader.ReadNullableDateTime("DateInstructed");
You should use
DateTime.TryParse Method
this not throw exception, like
var mydate =(DateTime)datetimeString
or
var mydate =DateTime.Parse(datetimeString)
does!!!
Try with the following code part
this._doa = (caseReader["DOA"] == DBNull.Value ? DBNull.Value : Convert.ToDateTime(caseReader["DOA"]);
Try to Convert Date Time as
this._doa = Convert.ToDateTime(caseReader["DOA"]);
I often deal with DBNull.Value...
So I use this method that will return the value of the object or the default value of the given value type if object's value is DBNull.Value.
public static object GetValueOrDefault(object value, Type type)
{
if (value != DBNull.Value)
return value;
if (type.IsValueType == false)
return null;
Array array = Array.CreateInstance(type, 1);
return array.GetValue(0);
}
Usage:
GetValueOrDefault(dataRecord.GetValue(fieldIndex), dataRecord.GetFieldType(fieldIndex)

Categories

Resources