Hi, I am getting an exeption
An exception of type 'System.ArgumentNullException' occurred in System.Data.dll was not handled in user code Additional information: The 'dataType' argument can not be null.
When executing the below code
[HttpPost]
public JsonResult Chart()
{
string constr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("select text,date from T"))
{
cmd.Connection = con;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
//Create a new DataTable.
DataTable dt = new DataTable("T");
List<object> iData = new List<object>();
//Load DataReader into the DataTable.
// dt.Load(sdr);
dt.Columns.Add(sdr["text"].ToString(), System.Type.GetType("System.String"));
dt.Columns.Add(sdr["date"].ToString(), System.Type.GetType("System.Datetime"));
foreach (DataColumn dc in dt.Columns)
{
List<object> x = new List<object>();
x = (from DataRow drr in dt.Rows select drr[dc.ColumnName]).ToList();
iData.Add(x);
}
//Source data returned as JSON
return Json(iData, JsonRequestBehavior.AllowGet);
}
}
}
Here is the image of the error
How can I resolve it? thanks.
You are passing NULL data in this line of the code
dt.Columns.Add(sdr["date"].ToString(), System.Type.GetType("System.Datetime"));
sdr["date"] should not be passed as Null value in the above code, always check if values are null or not and then execute this statement.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly