System.IndexOutOfRangeException: Cannot find table 0 - asp.net

I got struck with this issue since long and am unable to find a solution
I have been getting this error:
'System.IndexOutOfRangeException: Cannot find table 0.' at line 3:
if (Session["value"] != null)
{
**ds = proxy.GetId(Session["value "].ToString());**
if (ds!= null)
{
if (ds.Tables.Count == 0)
{
Response.Redirect("Timeout.aspx");
}
else
{
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
string Id = ds.Tables[0].Rows[0]["id"].ToString().Trim();
if (Id.Trim() == "0")
{
Session["ID"] = "ID NOT CREATED";
ds = proxy.Getid(Session["value "].ToString());
}
}
}
}
}
Ideally there is only value getting returned from the stored proc,so the error should not occur in line 3, since I put all checks for dataset 'ds' in below code. Request to please help me ...

Have you checked in the function GetId for possible causes for the error or if the session variable you're converting to a string has a value? I can see you getting that error if you're trying to turn a session var into a string when it hasn't been initialized.

Related

Loop program if user enters wrong input

I just started learning C# and while loops are confusing me. Unlike Java, where I can use a while loop to loop a program if a user entered a invalid input, it's not acting the same way in C#.
using System;
namespace first {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hi! What is your name");
string userName = Console.ReadLine();
Console.WriteLine("oh! you are:" + userName);
Console.WriteLine("let play a game");
string answer="Y";
while (answer == "Y") {
Random random = new Random();
int correntNumber = random.Next(1, 2);
int guess = 0;
Console.WriteLine("Guess a number");
while (guess != correntNumber) {
string userGuess = Console.ReadLine();
//validate input method 1
try {
guess = int.Parse(userGuess);
} catch (Exception e) {
Console.WriteLine("Invalid inout", e);
}
//validate input method 2
//if(!int.TryParse(userGuess, out guess)) {
// Console.WriteLine("invalid input");
//}
if (guess != correntNumber) {
Console.WriteLine("try again!");
}
}
Console.WriteLine("Yes! corrector");
Console.WriteLine("Play again?");
//string answer;
answer = Console.ReadLine().ToUpper();
if(answer == "Y") {
continue;
} else if (answer == "N") {
Console.WriteLine("bye");
return;
} else if (answer != "Y" || answer != "N") {
Console.WriteLine("y or n");
answer = Console.ReadLine().ToUpper();
continue;
}
}
}
}
}
When I enter a value other than y or n, the message appears,Console.WriteLine("Y or n only");, but the game restarts while it shouldn't.
I am sorry this is a simple and rather silly question, but I can't pin point where I am going wrong.
the problem is that after printing to the user "y or n only" message you take the input but you don't actually do anything with it
so the loop just restarts regardless of the input , to fix this issue you could replace the last if part with this code
while(answer != 'Y' && answer != 'N'){
Console.WriteLine("y or n only");
answer = Convert.ToChar(Console.ReadLine().ToUpper());
}
if(answer == 'Y')
{
continue;
}
else if(answer == 'N')
{
Console.WriteLine("goodbye");
return;
}
so after you read the first input answer of him for repeating or no you check if it's a valid input or not and if it's not you keep asking him for "y or n only" till he enters "Y" or "N" and then you process this answer for whether it's a "Y" or "N" in the if part

My page is adding extra query string parameter without any reason

I have a page which is usually called with id parameter to get the details for specific object, something like:
http://www.mywebsite.com/admin/productdetails.aspx?Id=1982
On page_load this is the code I have:
if (!Page.IsPostBack)
{
if (Request.QueryString[0] != null)
{
DataTable tbl = new DataTable();
tbl = hpl.getProductDetail(Convert.ToInt32(Request.QueryString[0]));
if (tbl.Rows.Count > 0)
{
}
}
else
{
Response.Redirect("ProductList.aspx");
}
}
And this is the error I'm getting http://prntscr.com/jd554l
also the URL adds extra parameter and it becomes http://www.mywebsite.com/admin/productdetails.aspx?Id=1982&Id=1982
Any idea what is happening?

Unable to check existing XML nodes in Adobe Flex 3

I am getting an XML data from server (please refer to xmlData value).
I need to:
Create another XML with non-duplicates Folders
Create another XML with final count on monthly basis.
I am unable to do this with below code and getting duplicate records.
private var xmlData:XML = new XML("<root><SUMMARY_RECORD><FOLDER>Folder1</FOLDER><COUNT>100</COUNT><MONTH>Feb</MONTH><QUARTER>Q1</QUARTER><YEAR>2014</YEAR></SUMMARY_RECORD><SUMMARY_RECORD><FOLDER>Folder1</FOLDER><COUNT>100</COUNT><MONTH>Feb</MONTH><QUARTER>Q1</QUARTER><YEAR>2014</YEAR></SUMMARY_RECORD></root>");
var folderDataXML:XML = new XML("<root></root>");
var folderDGDataXML:XML = new XML("<root></root>");
private function loaded():void{
var item:XML;
folderDGDataXML.appendChild(new XML("<FOLDER_NAME><Name>ALL</Name></FOLDER_NAME>"));
for each (item in xmlData.SUMMARY_RECORD){
if (folderDGDataXML.FOLDER_NAME.(Name==item.FOLDER).toString() == ""){
folderDGDataXML.appendChild(new XML("<FOLDER_NAME><Name>"+item.FOLDER+"</Name></FOLDER_NAME>"));
}
if (folderDataXML.SUMMARY_RECORD.(Name==item.MONTH).toXMLString() == ""){
folderDataXML.appendChild(new XML("<SUMMARY_RECORD><Name>"+item.MONTH+"</Name><COUNT>"+item.COUNT+"</COUNT></SUMMARY_RECORD>"));
}else{
var count:int = Number(folderDataXML.SUMMARY_RECORD.(Name==item.MONTH).COUNT) + Number(item.COUNT);
folderDataXML.SUMMARY_RECORD.(Name==item.MONTH).COUNT = count;
}
}
}
Final output, folderDGDataXML:
<root>
<FOLDER_NAME>
<Name>ALL</Name>
</FOLDER_NAME>
<FOLDER_NAME>
<Name>Folder1</Name>
</FOLDER_NAME>
<FOLDER_NAME>
<Name>Folder1</Name>
</FOLDER_NAME>
</root>
folderDataXML:
<root>
<SUMMARY_RECORD>
<Name>Feb</Name>
<COUNT>100</COUNT>
</SUMMARY_RECORD>
<SUMMARY_RECORD>
<Name>Feb</Name>
<COUNT>100</COUNT>
</SUMMARY_RECORD>
</root>
What am I doing wrong?
After getting correct XML, I need to populate datagrid & column chart.
Try this one
In XML Checking the elements present or not with hasOwnProperty or we can use in operator anyhow our case if it is based on value try like this way folderDGDataXML.descendants("FOLDER_NAME").(Name.text() == "Folder1"); //Return always XMLList. With the help of length() we can predict element with value occur or not.
Hope this will work for you.
var xmlData:XML = new XML("<root><SUMMARY_RECORD><FOLDER>Folder1</FOLDER><COUNT>100</COUNT><MONTH>Feb</MONTH><QUARTER>Q1</QUARTER><YEAR>2014</YEAR></SUMMARY_RECORD><SUMMARY_RECORD><FOLDER>Folder1</FOLDER><COUNT>100</COUNT><MONTH>Feb</MONTH><QUARTER>Q1</QUARTER><YEAR>2014</YEAR></SUMMARY_RECORD></root>");
var folderDataXML:XML = new XML("<root></root>");
var folderDGDataXML:XML = new XML("<root></root>");
var item:XML;
folderDGDataXML.appendChild(new XML("<FOLDER_NAME><Name>ALL</Name></FOLDER_NAME>"));
for each (item in xmlData.SUMMARY_RECORD)
{
var folderName:String = item.FOLDER.text();
var monthName:String = item.MONTH.text();
var existXMLList:XMLList = folderDGDataXML.descendants("FOLDER_NAME").(Name.text() == folderName);
if (existXMLList.length() == 0)
{
folderDGDataXML.appendChild(new XML("<FOLDER_NAME><Name>"+item.FOLDER+"</Name></FOLDER_NAME>"));
}
var monthXMLList:XMLList = folderDataXML.descendants("SUMMARY_RECORD").(Name.text() == monthName);
if (monthXMLList.length() == 0)
{
folderDataXML.appendChild(new XML("<SUMMARY_RECORD><Name>"+item.MONTH+"</Name><COUNT>"+item.COUNT+"</COUNT></SUMMARY_RECORD>"));
}
else
{
monthXMLList..COUNT = Number(monthXMLList..COUNT) + Number(item.COUNT);
//If more than one node it throws error. you need to use loop operation
}
}
trace("folderDGDataXML" + folderDGDataXML);
trace("folderDataXML" + folderDataXML);

Session Variables and List Object not Updating Correctly

I should be able to figure this out, but I've been fighting with it for quite a while now.
I have a popup page that has several available requests that a user can select from. Based upon the page that the user is accessing, there could be one request or multiple requests available for that user. The single and multiple request are both saved on different session variables.
I need to know the single request that the user selected at the beginning of the process. It works fine except when the user is allowed to add multiple requests, the single request session variable is also updated.
For example, single request variable has "Florida"; then, the user reaches the multiple request page and adds GA and LA to the multiple request session variable. The single request variable is also updated to include GA and LA even though the flag is false and never reached that line. I don't want it to be updated. I need that single request to be available at all the time, so the user can see it if and when requested.
Here is a sample code where the issue is happening:
List<Request> temp = new List<Request>();
List<Request> mySearchRequest = new List<Request>();
List<Request> listSingleRequest = new List<Request>();
if (SessionWrapper.currentRequest.AvailableRequests != null)
{
mySearchRequest = (List<Request>)SessionWrapper.currentRequest.AvailableRequests;
}
if (SessionWrapper.currentRequest.MultipleRequests != null)
{
temp = (List<Request>)SessionWrapper.currentRequest.MultipleRequests;
var test = temp.Find(delegate(Request req) { return req.RequestId == id && req.Desc == description; });
// Checking if we have on the container already
if (mySearchRequest.Any(r => r.RequestId == id && r.Desc == description) == false)
{
mySearchRequest.Add(test);
if (SessionWrapper.currentRequest.SingleRequest == true && mySearchRequest.Count() == 1)
{
listSingleRequest.Add(test);
SessionWrapper.currentRequest.singleRequest = listSingleRequest ;
listSingleRequest = null;
}
}
}
//Set multiple request session here
Your help is greatly appreciated.
Thanks,
JF
After playing around with it almost all night, I was able to fix it. I am not sure if that is the most efficient way of doing it, but it works for now.
if (SessionWrapper.currentRequest.MultipleRequests != null)
{
temp = (List<Request>)SessionWrapper.currentRequest.MultipleRequests;
var test = temp.Find(delegate(Request req) { return req.RequestId == id && req.Desc == description; });
// Checking if we have on the container already
if (mySearchRequest.Any(r => r.RequestId == id && r.Desc == description) == false)
{
mySearchRequest.Add(test);
if (SessionWrapper.currentRequest.SingleRequestPage == true && mySearchRequest.Count() == 1)
{
foreach (var item in test)
{
//Create a new request object and add it to the list
Request request = new Request();
request.RequestId == item.RequestId;
request.Description == item.Description;
listSingleRequest.Add(request);
}
SessionWrapper.currentRequest.singleRequest = listSingleRequest ;
listSingleRequest = null;
}
}
}
if (SessionWrapper.currentRequest.singleRequest != null)
{
tempRequest = SessionWrapper.currentRequest.singleRequest.ToList();
foreach (var test in tempRequest)
{
Request request = new Request();
request.RequestId == item.RequestId;
request.Description == item.Description;
listSingleRequest.Add(request);
}
SessionWrapper.currentRequest.ViewRequest = listSingleRequest;
listSingleRequest = null;
}

Linq to XML single or default

I am querying an xml and i am storing the results using singleordefault
var query = from nm in xelement.Descendants("EmployeeFinance")
where (int)nm.Element("EmpPersonal_Id") == empID
select new AllowancePaid
{
gradeTaxId = nm.Element("Allow-GradeTax").Elements("Amount").Attributes("BenListId").Select(a => (int)a).ToList(),
gradeTaxAmt = nm.Element("Allow-GradeTax").Elements("Amount").Select(a => (double)a).ToList()
};
Debug.WriteLine("2");
var resultquery = query.SingleOrDefault();
now this line: var resultquery = query.SingleOrDefault(); works fine if it found in the xml file. However, i have a case where my query will result in a null. If i have no value, it would make an entry in the xml file and my query obviously results in null. My question is how do i cater for this without causing my programe to crash. obviously, singleordefault() doesnt work.
***************** EDITED *************************
I read what everyone said so far and it make sense but i am still having a problem.
if (query.Count() == 0)
{
Debug.WriteLine("NULL");
}
else {
var resultquery = query.SingleOrDefault();
Debug.WriteLine("NOT NULL");
}
OR
if (query == null)
{
Debug.WriteLine("NULL");
}
else {
var resultquery = query.SingleOrDefault();
Debug.WriteLine("NOT NULL");
}
OR
var resultquery = query.SingleOrDefault();
if (resultquery == null)
{
Debug.WriteLine("NULL Result");
}
else
{
Debug.WriteLine("NOT NULL");
}
I am getting a System.NullReferenceException error when the first part of the if statement is true. One user said to do this: var resultquery = query.SingleOrDefault(); then use my if..else statement to do the comparison. However i am getting the error at the point of assign query.singleofdefault() to resultquery. So i am lost.. hope someone can help. thank you
what i am trying to understand is this. the documentation states if the result query is 0 it will give a default value, if it is not, it will be a single value. so why doesnt this give a default value? [taken from the comments]
null is the default value for reference types. Apparently AllowancePaid is a reference type (a custom class).
What is the value you want when the there is no value found.
You could either do:
if (resultquery == null) {
// Logic for No result
} else {
// Logic for result found
}
Or you could force a default value
eg.
var resultquery = query.SingleOrDefault() ?? new AllowancePaid();
UPDATE
From the comments posted it appears that the null reference exception is actually caused within the query itself rather than by the assignment to resultquery and use of later.
This updated query should solve the issue
var query = from nm in xelement.Descendants("EmployeeFinance")
where nm.Element("EmpPersonal_Id") != null
&& (int)nm.Element("EmpPersonal_Id") == empID
&& nm.Element("Allow-GradeTax") != null
&& nm.Element("Allow-GradeTax").Elements("Amount") != null
select new AllowancePaid
{
gradeTaxId = nm.Element("Allow-GradeTax").Elements("Amount").Attributes("BenListId").Select(a => (int)a).ToList(),
gradeTaxAmt = nm.Element("Allow-GradeTax").Elements("Amount").Select(a => (double)a).ToList()
};
var resultquery = query.SingleOrDefault();
if (resultquery == null) {
Debug.WriteLine("NULL Result");
} else {
// Logic here
}

Resources