I created a simple RESTful application in WCF(c#). When I'm populating using (GET) I've received this error
"Object reference not set to an instance of an object".
I received the error in the part of target.DocumentLines[0].itemCode = "";.
Here's my code:
public PRRequestData[] getAllPR()
{
List<PRRequestData> list = new List<PRRequestData>();
try
{
string sqlSelect = "SELECT DocEntry, Comments, ReqDate FROM OPRQ";
APP.strCommand = sqlSelect;
DataTable dt = new DataTable();
dt = APP.Ds.Tables[0];
foreach (DataRow row in dt.Rows)
{
// Person target = Activator.CreateInstance();
PRRequestData target = new PRRequestData();
target.requiredDate = row["ReqDate"].ToString();
target.remarks = row["Comments"].ToString();
target.docEntry = row["DocEntry"].ToString();
// DataColumnAttribute.Bind(row,target);
sqlSelect = "SELECT ItemCode, Quantity, Price, VendorNum, TaxCode FROM PRQ1 WHERE DocEntry = '" + row["DocEntry"].ToString() + "' ";
APP.strCommand = sqlSelect;
for (var i = 0; i < APP.Ds.Tables[0].Rows.Count; i++)
{
target.DocumentLines[0].itemCode = "";
}
list.Add(target);
}
return list.ToArray();
}
catch (Exception e)
{
e.ToString();
}
return list.ToArray();
Here's my DataContract source code also:
[DataContract(Namespace = "")]
public class PRRequestData
{
[DataMember]
public string docEntry { get; set; }
[DataMember]
public string remarks { get; set; }
[DataMember]
public string requiredDate { get; set; }
//[DataMember]
//public int rowcount { get; set; }
[DataMember]
public RequestDataDetails[] DocumentLines;
}
[DataContract]
public class RequestDataDetails
{
[DataMember]
public string itemCode { get; set; }
[DataMember]
public decimal quantity { get; set; }
[DataMember]
public decimal price { get; set; }
[DataMember]
public string supplier { get; set; }
[DataMember]
public string taxcode { get; set; }
}
Looks like you're not initialising this property
[DataMember]
public RequestDataDetails[] DocumentLines;
I recommend you to use List instead of RequestDataDetails[], as anyway you'll have to use an internal list.
Initialise the List of RequestDataDetails to fill it from the query, before the for loop.
List<RequestDataDetails> requestDetails = new List<RequestDataDetails>
Then change the for loop to add to that list instead of setting an array, im using the assignment you were doing, not sure if it'll do what you expect, just tell me if it suits your needs.
requestDetails.add(new RequestDataDetails { itemCode = "" });
instead of
target.DocumentLines[0].itemCode = "";
Then after the for loop convert the list to an array and assign it to target
target.DocumentLines = requestDetails.ToArray();
Hope it works!
Related
I have three classes
public class FeatureBook
{
public string Id { get; set; }
public String name { get; set; }
public String type { get; set; }
}
public class Feature
{
public String feature_id { get; set; }
public String value { get; set; }
}
public class Human {
public string Id { get; set; }
public bool validated { get; set; }
public List<Feature> features { get; set; }
public override String ToString() => Id;
}
Human has List<Feature>. Feature is linked to FeatureBook by feature_id
And I have DataGridView.
How can I get something like in the picture:
First of all, for such of functionality, i'd use 2 datagridviews with master-detail relationship.
Second of all, if you would like to bind data to single datagridview, you need to convert Feature's rows into columns. Here is complete sample (created with LinqPad):
void Main()
{
//create human list
List<Human> people = new List<Human>()
{
new Human(){Id = "H1", validated =false, features = new List<Feature>()
{
new Feature(){feature_id = "H1F1", value ="Feature 1"},
new Feature(){feature_id = "H1F2", value ="Feature 2"}
}},
new Human(){Id = "H2", validated =false, features = new List<Feature>()
{
new Feature(){feature_id = "H2F1", value ="Feature 1"},
new Feature(){feature_id = "H2F2", value ="Feature 2"},
new Feature(){feature_id = "H2F3", value ="Feature 3"},
new Feature(){feature_id = "H2F4", value ="Feature 4"},
new Feature(){feature_id = "H2F5", value ="Feature 5"}
}}
};
//create datatable
DataTable dt = new DataTable();
//add known columns (related to Human)
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Id", typeof(string)),
new DataColumn("validated", typeof(string))
});
//get max. of futures
int fc = people.Select(x=>x.features.Count).Max();
//add columns related to Feature
for(int i=0; i<fc; i++)
dt.Columns.Add(new DataColumn($"Feature {i}"));
//add data to datatable
foreach(Human h in people)
{
//add Human details
DataRow dr = dt.NewRow();
dr["Id"] = h.Id;
dr["validated"] = h.validated;
//add Feature details
for(int i=0; i<h.features.Count; i++)
{
Feature f = h.features[i];
dr[$"Feature {i}"] = f.value;
}
dt.Rows.Add(dr);
}
//datatable is ready to use
//dump its content ;)
dt.Dump();
}
// Define other methods and classes here
public class FeatureBook
{
public string Id { get; set; }
public string name { get; set; }
public string type { get; set; }
}
public class Feature
{
public string feature_id { get; set; }
public string value { get; set; }
}
public class Human
{
public string Id { get; set; }
public bool validated { get; set; }
public List<Feature> features { get; set; }
public override string ToString() => Id;
}
Note: there's few other ways to achieve that, but i wanted to show you the simplest way ;)
I'm trying to pass my CookieCarts string array (containing shopping cart items) into my controller to get looped for my Paypal api.
My View
var cookiecart = Server.UrlDecode(Request.Cookies["cookieCart"].Value);
#Html.HiddenFor(m => m.CookieCart, new { Value = cookiecart })
Response.Write(cookiecart);
cookiecart:*[{"datetime":"2016-02-25 02:51:49","id":"749","typeid":"13","qty":1,"fullname":"The Matrix","image":"/Content/images/products/online-video.png","price":"69","sku":"MATRIX"}]*
My Model
public string CookieCart { get; set; }
My Controller
var cartArray = model.CookieCart;
var cartArray = model.CookieCart;
var itemArray = cartArray.Split(',');
foreach (var t in itemArray)
{item.name = itemArray[0]; }
when i quickwatch the data sent to the controller it looks like this:
cartArray displays: "[{\"datetime\":\"2016-02-25 02:51:49\",\"id\":\"749\",\"typeid\":\"13\",\"qty\":1,\"fullname\":\"The Matrix\",\"image\":\"/Content/images/products/online- video.png\",\"price\":\"69\",\"sku\":\"MATRIX\"}]"
item.name displays: *"[{\"datetime\":\"2016-02-25 02:51:49\""*
None of this is right. its so frustrating! How to convert a cookie array value into a C# array.
itemArray[0] should be:
itemArray[0][0] = datetime:"2016-02-25 02:51:49",
itemArray[0][1] = id:"749",
itemArray[0][2] = typeid:"13",
itemArray[0][3] = qty:1,
itemArray[0][4] = fullname:"The Matrix",
itemArray[0][5] = image:"/Content/images/products/online-video.png",
itemArray[0][6] = price:"69"
itemArray[0][7] = sku:"MATRIX"
:(
ok i figured it out. using JSON .Net:
My Controller
var cookie = Request.Cookies["cookieCart"];
cookieArray = JsonConvert.DeserializeObject<List<CookieCart>>
(Server.UrlDecode(cookie.Value));
My Model
public class CookieCart
{
public DateTime Datetime { get; set; }
public int Id { get; set; }
public int Typeid { get; set; }
public string Qty { get; set; }
public string Fullname { get; set; }
public string Image { get; set; }
public string Price { get; set; }
public string Sku { get; set; }
}
then i iterated the array items for PayPal:
foreach (var cartitem in cookiecart)
{
item.name = cartitem.Fullname;
item.currency = "USD";
item.price = cartitem.Price;
item.quantity = cartitem.Qty;
item.sku = cartitem.Sku;
var intPrice = Int32.Parse(cartitem.Price);
subtotal = subtotal + intPrice;
}
I'm currently working on parsing a csv file that was exported by another application. This application exported the data in a strange way. This export is from accoutning and it looks similar to this..
I'm trying to figure out a way to read the csv file, then split up the multiple 'All Accounts' values and 'Amt' Values so that M200 and 300.89 is another entry, M300 and 400.54 are another entry, and M400 and 100.00 are another entry. So after inserting this single row into the database, I should actually have 4 rows like so..
This is how I'm currently reading and inserting into the database.
List<RawData> data = new List<RawData>();
try
{
string text = File.ReadAllText(lblFileName.Text);
string[] lines = text.Split('\n');
int total = 0, reduced = 0;
foreach (string line in lines)
{
RawData temp = new RawData(line);
total++;
if (!(temp.FirstAccount.Length == 0 || temp.FirstAccount == "1ST-ACCT-NO"))
{
reduced++;
data.Add(temp);
}
}
}
catch (IOException ex)
{
Console.WriteLine("Unable to read file. " + ex.ToString());
MessageBox.Show(ex.ToString());
}
try
{
foreach (RawData rData in data)
{
tCarsInTransit cit = new tCarsInTransit
{
FIRST_ACCT_NO = rData.FirstAccount,
ACCOUNT_NO_DV = rData.AccountNoDv,
ACCT_NO = rData.AcctNo,
ACCT_NO_L = rData.AccNoL,
ACCT_NUM_DV = rData.AcctNumDv,
ACCT_PFX = rData.AcctPfx,
ACCT_PFX_PRT = rData.AcctPfxPrt,
ACCT_TYPE_DV = rData.AcctTypeDv,
ADV_NO = rData.AdvNo,
ALL_PRT_FLAG = rData.AllPrtFlag,
AMT = rData.Amt,
AMT_GLE = rData.AmtGle,
BASE_GLE = rData.BaseGle,
CNT_CAT = rData.CntCat,
COLD_PRT_FLAG = rData.ColdPrtFlag,
COST_DV = rData.CostDv,
COST_OVRD_FLAG_DV = rData.CostOvrdFlagDv,
CR_ACCT_DV = rData.CrAcctDv,
CR_ACCT_DV_GLE = rData.CrAcctDvGle,
CROSS_POSTING_FLAG = rData.CrossPostingFlag,
CROSS_POST_CAT = rData.CrossPostCat,
CTRL_NO = rData.CtrlNo,
CTRL_TYPE_DV = rData.CtrlTypeDv,
DESC_REQD_DV = rData.DescReqdDv,
DR_ACCT_DV = rData.DrAcctDv,
GL_DIST_ACCT_DV = rData.GLDistAcctDv,
GL_DIST_DV = rData.GLDistDv,
GRP_NO_DV = rData.GrpNoDv,
ID_PORT_DATE_TIME_FMT_CAT = rData.IdPortDateTimeFmtCat,
INACTIVITY_DV = rData.InactivityDv,
JOIN_COL = rData.JoinCol,
JRNL_DATE = rData.JrnlDate,
JRNL_PFX = rData.JrnlPfx
};
tCIT.tCarsInTransits.Add(cit);
tCIT.SaveChanges();
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Finished uploading. ";
}
}
catch (DbEntityValidationException ex)
{
foreach (var eve in ex.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
I am not sure how to accomplish this. The above currently inserts the csv file into Sql Server the exact way the csv file was exported. Any ideas would greatly be appreciated! Thanks!
EDIT: Here is the RawData class.
class RawData
{
public string FirstAccount { get; set; }
public string AccountNoDv { get; set; }
public string AcctNo { get; set; }
public string AccNoL { get; set; }
public string AcctNumDv { get; set; }
public string AcctPfx { get; set; }
public string AcctPfxPrt { get; set; }
public string AcctTypeDv { get; set; }
public string AdvNo { get; set; }
public string AllPrtFlag { get; set; }
public string Amt { get; set; }
public string AmtGle { get; set; }
public string BaseGle { get; set; }
public string CntCat { get; set; }
public string ColdPrtFlag { get; set; }
public string CostDv { get; set; }
public string CostOvrdFlagDv { get; set; }
public string CrAcctDv { get; set; }
public string CrAcctDvGle { get; set; }
public string CrossPostingFlag { get; set; }
public string CrossPostCat { get; set; }
public string CtrlNo { get; set; }
public string CtrlTypeDv { get; set; }
public string DescReqdDv { get; set; }
public string DrAcctDv { get; set; }
public string GLDistAcctDv { get; set; }
public string GLDistDv { get; set; }
public string GrpNoDv { get; set; }
public string IdPortDateTimeFmtCat { get; set; }
public string InactivityDv { get; set; }
public string JoinCol { get; set; }
public string JrnlDate { get; set; }
public string JrnlPfx { get; set; }
public RawData(string csvString)
{
string[] citData = csvString.Replace(", ", "").Replace(".,", ".").Split(',');
try
{
FirstAccount = citData[0];
AccountNoDv = citData[1];
AcctNo = citData[2];
AccNoL = citData[3];
AcctNumDv = citData[4];
AcctPfx = citData[5];
AcctPfxPrt = citData[6];
AcctTypeDv = citData[7];
AdvNo = citData[8];
AllPrtFlag = citData[9];
Amt = citData[10];
AmtGle = citData[11];
BaseGle = citData[12];
CntCat = citData[13];
ColdPrtFlag = citData[14];
CostDv = citData[15];
CostOvrdFlagDv = citData[16];
CrAcctDv = citData[17];
CrAcctDvGle = citData[18];
CrossPostingFlag = citData[19];
CrossPostCat = citData[20];
CtrlNo = citData[21];
CtrlTypeDv = citData[22];
DescReqdDv = citData[23];
DrAcctDv = citData[24];
GLDistAcctDv = citData[25];
GLDistDv = citData[26];
GrpNoDv = citData[27];
IdPortDateTimeFmtCat = citData[28];
InactivityDv = citData[29];
JoinCol = citData[30];
JrnlDate = citData[31];
JrnlPfx = citData[32];
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong. " + ex.ToString());
}
}
}
EDIT 2: AllAccounts in the images is acutally 'AccountNoDv' and there are actually many different fields that have multiples like 'AccountNoDv'(AllAccounts) but we might be removing those as this is not a final export. As of right now the two fields I'm worried most about are AccountNoDv and Amt.
Try something like this:
foreach (string line in lines)
{
RawData temp = new RawData(line);
var AllAccounts = temp.AccountNoDv.split(' ');
var Amts = temp.Amt.split(' ');
if (AllAccounts.Length() == Amts.Length() && Amts.Length() > 1) {
// We have multiple values!
reduced++;
for (int i = 0; i < AllAccounts.Length(); i++) {
RawData temp2 = RawDataCopy(temp); // Copy the RawData object
temp2.AccountNoDv = AllAccounts[i];
temp2.Amt = Amts[i];
total++;
data.Add(temp2);
}
}
else {
total++;
if (!(temp.FirstAccount.Length == 0 || temp.FirstAccount == "1ST-ACCT-NO"))
{
reduced++;
data.Add(temp);
}
}
}
And:
private RawData RawDataCopy(RawData copyfrom) {
// Write a function here that returns an exact copy from the one provided
// You might have to create a parameterless constructor for RawData
RawData RawDataCopy = new RawData();
RawDataCopy.FirstAccount = copyfrom.FirstAccount;
RawDataCopy.AccountNoDv = copyfrom.AccountNoDv;
RawDataCopy.AcctNo = copyfrom.AcctNo;
// . . . . . . . .
RawDataCopy.JrnlPfx = copyfrom.JrnlPfx;
return RawDataCopy;
}
Then also add a parameterless constructor to your RawData class:
public RawData()
{
}
Perhaps it would be sexier to implement the ICloneable interface and call the Clone() function instead of the RawDataCopy function, but it gets the idea across.
In Linq you can use SelectMany to increase the number of elements in a list. Here is a rough example of how this could be done. I make the assumption that the number of sub elements in AllAccounts and Amt is the same. A more robust solution would check for these issues.
So after you have loaded your data list:
var expandedData =
data.SelectMany(item =>
// split amount (will just return one item if no spaces)
item.Amt.Split(" ".ToCharArray())
// join this to the split of all accounts
.Zip(item.AllAccounts.Split(" ".ToCharArray()),
// return the joined item as a new anon object
(a,b) => new { amt=a, all=b }),
// take the original list item and the anon object and return our new item
(full,pair) => { full.Amt = pair.amt; full.AllAccounts = pair.all; return full; }));
You will now have a list of your data items with the multiple items expanded into the list.
I don't have test data to test so I might have some minor typos -- but I put in lots of comments to make the Linq as clear as possible.
Here is is simple example I wrote in LinqPad for myself to make sure I understood how SelectMany worked:
string [] list = { "a b c d","e","f g" };
var result = list.SelectMany((e) =>
e.Split(" ".ToCharArray()),
(o,item) => new { org = o, item = item}).Dump();
I've been trying to add to a list of lists adding information from an sql db. The error I get when adding new info to a list is this:
"An exception of type 'System.NullReferenceException' occurred in StatusScope.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object."
How would I solve this error?
MapLogic.cs
public static List<MapModel.ClientInfo> GetClientsData()
{
SqlConnection Connection = site.Models.Shared.DBConnection.GetConnection();
SqlDataReader Reader = null;
SqlCommand Command = new SqlCommand("SELECT DocInfo.DocID, DocInfo.DocName, DocInfo.DocPic, DocInfo.PatientAcceptance, ClientInfo.ClientName, ClientInfo.AddressLocal, ClientInfo.AddressBroad, ClientInfo.Phone, ClientInfo.Lat, ClientInfo.Long, ClientInfo.ClientID FROM DocInfo INNER JOIN ClientInfo ON DocInfo.ClientID = ClientInfo.ClientID;", Connection);
Reader = Command.ExecuteReader();
var ClientsData = new List<MapModel.ClientInfo> { };
int IDCounter = 0;
bool FirstRun = false;
while (Reader.Read())
{
if (!FirstRun)
{
ClientsData.Add(new MapModel.ClientInfo { Id = IDCounter, ClientID = Reader["ClientID"].ToString(), ClientName = Reader["ClientName"].ToString(), DocPic = Reader["DocPic"].ToString(), PatientAcceptance = Reader["PatientAcceptance"].ToString(), AddressLocal = Reader["AddressLocal"].ToString(), AddressBroad = Reader["AddressLocal"].ToString(), Phone = Reader["Phone"].ToString(), latitude = Reader["Lat"].ToString(), longitude = Reader["Long"].ToString(), DocNames = { } });
FirstRun = true;
}
else
{
for (var x = 0; x < ClientsData.Count; x++)
{
if (ClientsData[x].ClientID == Reader["ClientID"].ToString())
{
ClientsData[x].DocNames.Add("123"); //error occurs here
}
else
{
ClientsData.Add(new MapModel.ClientInfo { Id = IDCounter, ClientID = Reader["ClientID"].ToString(), ClientName = Reader["ClientName"].ToString(), DocPic = Reader["DocPic"].ToString(), PatientAcceptance = Reader["PatientAcceptance"].ToString(), AddressLocal = Reader["AddressLocal"].ToString(), AddressBroad = Reader["AddressLocal"].ToString(), Phone = Reader["Phone"].ToString(), latitude = Reader["Lat"].ToString(), longitude = Reader["Long"].ToString() });
}
}
}
IDCounter++;
}
Connection.Close();
return ClientsData;
}
MapModel.cs
public class ClientInfo
{
public int Id { get; set; }
public string ClientID { get; set; }
public string ClientName { get; set; }
public List<string> DocNames { get; set; }
public string DocPic { get; set; }
public string PatientAcceptance { get; set; }
public string AddressLocal { get; set; }
public string AddressBroad { get; set; }
public string Phone { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
}
Do you ever set DocNames to anything? If not, it will be null.
In the constructor for ClientInfo you should have
DocNames = new List<string>();
to initialize an empty list.
Isn't it the case of testing whether ClientsData[x].DocNames is null before adding? You could change your code to
if(ClientsData[x].DocNames == null) {
ClientsData[x].DocNames = new List<string>();
}
ClientsData[x].DocNames.Add("123");
so that it always works.
In both lines where you add a new MapModel.ClientInfo to your ClientsData list, you need to initialize DocNames to a new List() otherwise whenever you try to access DocNames, it's always null. So, in both places you need to do the following:
ClientsData.Add(new MapModel.ClientInfo
{
Id = IDCounter,
//.
//. removing code just to make my answer shorter
//.
DocNames = new List<string>()
});
Given this document class:
public class Tea
{
public String Id { get; set; }
public String Name { get; set; }
public TeaType Type { get; set; }
public Double WaterTemp { get; set; }
public Int32 SleepTime { get; set; }
}
public enum TeaType
{
Black,
Green,
Yellow,
Oolong
}
I store a new Tea with the following code:
using (var ds = new DocumentStore { Url = "http://localhost:8080/" }.Initialize())
using (var session = ds.OpenSession("RavenDBFirstSteps"))
{
Tea tea = new Tea() { Name = "Earl Grey", Type = TeaType.Black, WaterTemp = 99d, SleepTime = 3 };
session.Store(tea);
session.SaveChanges();
Console.WriteLine(tea.Id);
}
The tea will be successfully saved, but when I try to query all black teas with linq, I am getting no results:
using (var ds = new DocumentStore { Url = "http://localhost:8080/" }.Initialize())
using (var session = ds.OpenSession("RavenDBFirstSteps"))
{
var dbTeas = from teas in session.Query<Tea>()
where teas.Type == TeaType.Black
select teas;
foreach (var dbTea in dbTeas)
{
Console.WriteLine(dbTea.Id + ": " + dbTea.Name);
}
}
I also tried to save the Enum as Integer with the following command:
ds.Conventions.SaveEnumsAsIntegers = true;
But, the result is the same. All works when I use the Name or the WaterTemp. Does RavenDB supports Enums in this way or I am totally wrong?
It seemed that I got the answer. It is always not recommended to use properties with a name like Type, which can be a reserved keyword.
I renamed Type and everything works, so the answer is:
public class Tea
{
public String Id { get; set; }
public String Name { get; set; }
public TeaType TeaType { get; set; }
public Double WaterTemp { get; set; }
public Int32 SleepTime { get; set; }
}