I have datareader which reads through 5 names with different values attached to each name.
While debugging i can get to each name using this expression computed by VS:
(new System.Collections.Generic<System.IO.FileInfo>(((School.PackageReader)(reader))._incomingStudents)).Items[0].Name
This above code while debugging gives me values i need like 5 different names when i change Items from 0 to 1 or 2... But when i try to use above code in my .cs file i get errors. Is there anyway to use this in code and get the values?
The error you are receiving is because you did not define the type of Collection you wanted to use.
I assume you are looking for a List, in which case you need to instantiate it this way:
(new System.Collections.Generic.List<System.IO.FileInfo>(((School.PackageReader)(reader))._incomingStudents)).Items[0].Name
Though it doesn't seem like this would be a good case for a List as you are calling a single value in Items[0].Name. BUT that's where your error is coming from.
Related
I have a button in the UI that reveals all remaining records in a table.
To handle this, in my controller, I have a simple SQL SELECT * statement with a LIMIT and OFFSET. My ExecuteReader is currently returning my data from the SQL command, which I am adding to a List. The list contains instances of my custom Run class.
In SSMS, the SQL query executes without exception no matter how large of a LIMIT I request. If my limit is > the number of rows in the table, it just returns all rows.
In webAPI, though, when my limit is > 200, it returns an exception. Otherwise, when less than 200, it returns a List of Runs without exception. I'm trying to debug the exception that occurs when I try to return all the data, but when it passes to the catch block, the exception is null. Which is weird.
So, I think there is a step I'm missing. Maybe I shouldn't be transforming the data into the Run class while the Reader is streaming. If I verified that the SQL command is accurate, then this seems to be the step that is causing the bug. Maybe transforming the data is making the Reader sorta time out? I don't understand ExecuteReader well enough to be able to figure out how I can pass all the data to List and then transform the data in that list into Runs after closing the connection. And don't even know if that would solve problem anyway.
All misgivings about potential SQL injections and lack of dbContext, etc. aside, how can I return all my records from the database utilizing ExecuteReader()?
Thanks.
Edit to add:
My exception value in the catch block is {"Data is Null. This method or property cannot be called on Null values."}.
In the debugger output, I my exception Exception thrown: 'System.Data.SqlTypes.SqlNullValueException' in Microsoft.Data.SqlClient.dll.
Edit to comment on the solution.
Ann L. figured this out. I had null values coming from the database. I learned from her and PSGuy that I can check for null values by using DbNull. Thank you!
Note - an easy place to get tripped up is that your class has to allow for nulls or else VS won't allow you to check for nulls in the method in the controller.
Here's one approach to the syntax you'll need to use (although there are lots of other approaches: see here for a bunch of alternatives!)
shoeAge = reader.IsDBNull(13) ? null : reader.GetInt64(13)
This assumes shoeAge is a nullable Int64. If it isn't, you'll get another error since you won't be able to assign null to it.
So these days I am trying to work with the TFS API. So far it was good, but all of a sudden.. I want to retrieve specific story's work items and their respective information using a search by ID method to pick the correct story. In order not to miss some important information I am doing SELECT * in my queries. I get the story, I get the Tasks.. But there seems to be problem with few of the fields - namely AreaPath, IterationPath and Type. As a primitive check I've written down some Console prints to check what's good and what's not - so if I uncomment any of the three previously named on execution this exception is thrown: A first chance exception of type 'Microsoft.TeamFoundation.WorkItemTracking.Client.FieldDefinitionNotExistException' occurred in Microsoft.TeamFoundation.WorkItemTracking.Client.dll.
Here is what I am trying to print out:
Console.WriteLine(target.Fields["Title"].Value);
Console.WriteLine(target.Fields["Description"].Value);
Console.WriteLine(int.Parse(target.Fields["Id"].Value.ToString()));
Console.WriteLine(target.Fields["AreaPath"].Value); //Problem 1
Console.WriteLine(target.Fields["IterationPath"].Value); //Problem 2
Console.WriteLine(int.Parse(target.Fields["AreaId"].Value.ToString()));
Console.WriteLine(int.Parse(target.Fields["IterationId"].Value.ToString()));
Console.WriteLine(target.Fields["State"].Value);
Console.WriteLine(target.Fields["Type"].Value.ToString()); //Problem 3
With or without ToString() nothing really changes.
Any suggestions ?
EDIT: They are not null, I've checked while in Debug mode, they all have assigned values.
Use CoreField or builtin getters:
Console.WriteLine(target.Fields[CoreField.Title].Value);
Console.WriteLine(target.Fields[CoreField.AreaPath].Value);
Console.WriteLine(target.State);
Console.WriteLine(target.Type.Name);
I'm reading a tab delimited text file into a String[]. Then, go thru the array line by line, split it into individual elements (currentLine.Split('\t')), make changes to the elements as needed, and then do a Parameters.Add to add each element as a parameter to the query string.
For the most part, it works and has added stuff to the Access table. However, it hit something in the data that it didn't like, and I'm having trouble determining which element is causing the data mismatch. The only error I'm getting (VS Express 2012) is Data type mismatch in criteria expression.
Is there a way to see which parameter is causing the error? I can tell which line it is by looking at what's already been added to the table, but I don't see where the problem is.
Thanks!
Your question seems pretty general although here are some techniques that will help you resolve it:
Wrap your database insert/update statement in a try / catch. Within the catch block write the parameters that were active at time of the insert.
Inspect the exception for an inner exception that will likely have more specific details of the invalid parameter/value.
I'm trying to get data from SAP via SAP Connector 3.0 on a MVC3 application.
There is no problems with the connection.
My problem is when I try to set values on a structure from a table it says
"TABLE [STRUCTURE ZHRS_ABSENCES]: cannot set value (array storing element values is null)"
My code is the following:
//create function
IRfcFunction function = conex.Repository
.CreateFunction("Z_HR_PORTAL_GET_EMPLOYEE_DATA");
//get table from function
IRfcTable absenceHoli = function.GetTable("P_ABSENCES");
//setting value to structure
absenceHoli.SetValue(0, "0000483"); //this is where the error occurs
I'm not sure about the connector you're using, but there's a similar common misunderstanding when using JCo. A table parameter can hold multiple lines. You'll usually have to append a line to the table. This will probably return some kind of structure that you'll be able to fill. Also check this answer.
I think you just need to Append a new row before trying to call SetValue
e.g.
absenceHoli.Append();
absenceHoli.SetValue("ColumnName", "0000483"); // Add further SetValue statements for further columns
You can get the column names by putting a breakpoint on after you're got the Table Structure and examining it, which is probably nicer than just specifying column indexes.
In my case I needed to use Insert:
absenceHoli.Insert();
absenceHoli.SetValue(..., ...);
I started with this question: (how-to-compare-liststring-to-db-table-using-linq). Using the solution offered there, it worked for my test with only 5 items in my list.
I'm having an issue with this now. I'm getting a SQL error saying "error near 0".
After using a LINQ to SQL visualizer, the problem is when Visual Studio is assigning the numbers in the list to the parameters. It will assign the first 10 just fine.
Once it hit the 11th, it jumps to the number at index 1 and assigns the same number 10 times to different params, but adding a last digit(0-9) to make each one unique. It then moves to the number at index 2 and proceeds to assign that same number multiple times.
It actually runs to the the end of the declared params, but I would imagine if there were 100 params declared it would continue.
var custdata = from c in db.CUSTs
where tnbrs.Contains(c.NPA + c.NXX + c.LINE_NBR)
select new { c.PON, c.PartnerID };
What is going on here?
So apparently its the LINQ to SQL visualizer, procured from Scott Gu's blog, that has the problems with assigning the parameters correctly.
I used this class with the System.Diagnostics to log the the LINQ to SQL debug.
If you use the class above be sure to change the namespace to match the one declared in your project.