I have stored a variable as TempData["ReqNo"]
ViewBag.TransNum = TempData["ReqNo"];
TempData.Keep();
I have a jQuery DataGrid Table that I populate with
var q = db.ICS_Orders;
I have been trying to add a WHERE Clause using the value of TempData but no matter what I try, I cannot get it to work. I have looked all over, online, and found very little help.
Was hoping someone could just give me a quick example of how to use TempData in linq. I have the following
var currReq = TempData["ReqNo"];
q = q.Where(s => s.RequisitionNumber.Contains(currReq)).ToList();
The above is one of MANY variations I have tired. With the above, I am getting the error "cannot convert object to string"
I tried to convert using .
Convert.ToString(currReq);
But that didn't help at all.
And to answer the question on everyone's mind :Why are you doing it like this?
Because, I am a newbie and I am finding my way. I usually try to figure things out the best I can before asking for help. I really can't find much information about using TempData values in linq online.
Thank you
To get variables back out of TempData, cast them back to their original type. So if TempData["ReqNo"] was assigned a string, you can get it back out with:
var currReq = (string)TempData["ReqNo"];
Convert.ToString(foo) or foo.ToString() are for getting the string representation of a non-string object. Since the object in TempData["ReqNo"] was originally a string, you don't need to use a convert function, just a cast back to the original type.
Related
There are a few similar questions, but none of them seem to answer my problem.
In my application I use lots of different SELECT queries and I don't want to write a method for every one, so I'm trying to create a generic one that I can just pass the specific parameters in.
This is the specific example that I'm trying to modify:
SELECT Statement
This is my attempt at generalising it:
Generic SELECT Statement
I have considered trying to split up columns string by the comma delimiter, using list or arrays, but I can't work out how to solve this issue. The specific bit that I want help with is the line with the error ("cannot convert from 'System.Collections.Generic.List' to 'int'"), but I don't know enough about data readers to fix it...
Any help would be greatly appreciated.
I have a doubt, why do you need to pass columnNames as array of List<string>.
array is not needed what I feel. Try passing just the List or array of string and let me know if it worked...
Part of our solution is a page that displays company-specific information using an ASP Gridview. Our method of constructing the SQL that feeds the Gridview is by using C# to build a custom SELECT statement based on a series of user inputs.
Once the user applies their filters through a button click, C# loops through all of their selections (check boxes and text boxes) and then propagates those selections to a separate method which constructs a WHERE clause to append to a simple SELECT statement. We use a Table-Valued Function in the FROM statement, and the only input parameter is from the Querystring and this does not change throughout the process.
Once the query has been assembled using C#, we apply this query to the SqlDataSource as the Select Command. However, we have recently discovered a very bizarre SQL error that we haven’t seen before:
Errors :
"The variable name '#' has already been declared.
Variable names must be unique within a query batch or stored procedure."
We aren’t declaring any variables in our SQL. As stated above, the only input parameter comes from the Querystring, and we access this parameter using both QueryStringParameters in the ASP:SqlDataSource on the ASP side and “int.Parse(Request.QueryString["id"]).ToString()” on the C# side while constructing the SQL query.
After researching this error, I have yet to find an instance where the variable declaration is empty. Most people are getting errors similar to this when they have declared a variable such as '#email' or '#address' twice. We have no double declarations, and the fact that the variable in the error is not defined is causing a massive headache.
Has anyone seen anything like this before or have any suggestions on how to further debug?
I'll post some code if need be, but we are mostly interested to see if anyone has seen an error like this before.
Code:
string MainQueryStr = ResultsPages.SearchString(SearchVariables(), Request,
ProjectsSqlds, 0, "SELECT DISTINCT dbo.{0}.* FROM dbo.{0}(" + int.Parse(Request.QueryString["id"]).ToString() + ")",
"getXyzById", "AbcId");
StringBuilder SearchQueryStr = new StringBuilder();
SearchQueryStr.Append(MainQueryStr);
SearchQueryStr.Append(" ORDER BY AbcName");
ProjectsSqlds.SelectCommand = SearchQueryStr.ToString();
The search string function is a 500 line method that we can't post right now. It is used all over our solution and works as it should. It stitches together strings to create the query.
This is how the SearchString function appends the parameters:
l.Add(ResultsPages.NewSearchQueryString(ABCFiltersTxBx, SearchQueryStringVariableType.String,
"{1}.AbcID IN (" + ABCFiltersTxBx.Text + ")"));
Where the ABCFiltersTxBx is parsed into a comma separated string.
I should chime in as the supervisor in question here:
OK, so we figured out what was happening.
What we didn't realize was that the SQLDataSource was taking our appended WHERE clauses and using them as SelectParameters. Each parameter we wanted to add to the query that would ultimately feed the SQLDS was then being added as a SelectParameter without us realizing it, and because we hadn't made any explicit parameter declarations, the parameters were added with just "" as the name, leading to the error of "'#' has already been declared".
The most embarrassing part of this whole thing is that our API has already accounted for Parameter Names, but we had unwittingly excluded this part. Thank you all very much for reading and attempting to help. We thoroughly appreciate you taking your time to help us brainstorm our solution over here.
So I suppose the take-home of this whole error is in 2 parts:
Know your API. When you realize that you screwed it up on your own, graciously thank those that took the time to help you here on StackOverflow (or wherever you seek help), as their time is valuable as well.
"'#' is already declared" would indicate that you have parameters being declared without a name, so when debugging, look through the SQLDS you are using and find any parameters that haven't been explicitly named.
Again, thank you to all who read and offered to help. It's greatly appreciated.
I am having to call a ColdFusion web service which returns a QueryBean. Which returns a set of complex arrays that have the values I am looking for. When I make the call I get all the data in the arrays that I am expecting but now I am trying to get to the data and it keeps telling me that I am missing something or I can't use indexing with an object.
I created a web reference called DAM_Search in VS2010 and I call it with this command;
DAM_Folder.folderService obj_Folder = new DAM_Folder.folderService();
DAM_Folder.QueryBean qBean = bj_Folder.getfolder("1-Key", str_Folder_ID);
What I get back is qBean which contains 4 arrays in it, with the first two arrays are
qBean.columnList[10]
qBean.columnListField[10]
and contain the names of the fields that are used in the data rows. Next it has
qBean.data[4]
qBean.dataField[4]
This is where the data is returned. In the example above the qBean in returning 4 rows of data. The data is in an array with the values of the columns named in the columnList[10].
I figure that since a picture is worth a 1000 words, this might help.
(source: sapp-family.com)
What I am trying to do now is get the data from the internal array of qBean.data[0][0]. In the Immediate Window when I try ? qBean.data[0] it returns the correct array of values.
I have tried the following
? qBean.data[0][0]
"Cannot apply indexing with [] to an expression of type 'object'"
? qBean.data[0].[0]
Identifier expected
? qBean.data[0,0]
Wrong number of indices inside []; expected 1
So, my fellow Stackoverflow users, what do I need to do for getting the values of this object? I figure it's something simple and I am just over thinking it, but it is just driving me up a wall right now.
I would try qBean.data[0][0].toSting()
Ok, So I got side tracked and did not go back and update the original question with a work around. Sorry about that...
My solution to the issue is to cast the qBean.Data[0] as an IEnumerable object. I then run it through a foreach loop which feeds the data into an array that I can pass back to the calling function. For example;
string[] FileData = new string[0];
foreach (var var_Value in (IEnumerable)qBean.data[0])
{
Array.Resize<string>(ref FileData, FileData.Length + 1);
FileData[FileData.Length-1] = var_Value.ToString();
}
Now that I know this works, I am going to pull a count from the other array in the QueryBean and set the FileData[] size before I start the foreach so I don't have to copy the array 21 times with the Array.Resize<>. While at the moment the data I am working with is small, I never know what it's going to be in the future.
Tim
It is VS 2010 version.
CR is able to accept NULL values, because when it prompts me for parameter values, there is cute little check box which says 'Set to NULL'. This is behavior that I need.
But, I almost never call reports like that, I do it in code. So, if a user does not provide a value for a parameter, I would like to pass NULL value in its place, so I could do something differently in report itself.
I've searched around net for ages, mostly in CR forums, and I've got number of solution, non of which actually works.
This is one of them:
ParameterDiscreteValue objDiscreteValue = new ParameterDiscreteValue();
objDiscreteValue.Value = null;
rptH.SetParameterValue(parName, new ParameterDiscreteValue(objDiscreteValue));
CR says you haven't provided a value for ... parameter.
I muscle solve it by creating double parameters. One real parameter, and one satellite parameter for it (boolean value, only to see if there is a value for real parameter). And it works. But it is tedious, and makes my generics (automatic prompting for parameters based on report metadata) very very hard.
I am hoping someone has dug deep enough to find a solution for this problem.
Regards,
I did this with VS.2005, but i think it's the same problem. From experience, we didn't have any trouble with VARCHAR data type, but the problem arises when we use the Number data type.
For VARCHAR Data Type, you just pass an empty String to the parameter. For example, I have parameter within SQL Server with data type VARCHAR as below:
MyParamFields.Add(AddParamReport("#TheActiveYear", mTahunAktiv, ParameterValueKind.StringParameter))
In this case, if I want to pass NULL value I just give empty string to mTahunAktiv variable within my VB.Net Code, like this mTahunAktiv="". and the CR suggest this as null value and give the right result.
For kind of Number Data Type, it's little tricky. I think this is the best way for now, same as you I've googled but never know the solution yet.
For example in my case I have a parameter for passing Month in number value with data type SMALLINT, but sometimes a user needs the value as NULL. To overcome the problem I just send a value, example 13 to that parameter, but before that you must handle it in the SQL QUERY, if you give the 13 value to that parameter it becomes NULL. For this, I am using STORED PROCEDURE with dynamic Filtering Parameter. For me this can solve my problem.
I'm not sure if you will understand what I mean, but I hope this little exprience can help you to solve your problem.
I stumbled upon DBValue.Null from ADO.NET DataSets and it sparked me to try to pass that to Crystal, and voila, it works. Just like this:
ParameterDiscreteValue objDiscreteValue = new ParameterDiscreteValue();
objDiscreteValue.Value = DBNull.Value;
rptH.SetParameterValue(parName, objDiscreteValue);
I hope this helps to someone, but I think this should be documented somewhere.
Best Regards,
Hello I have an object which has value "B11" i.e
object data = "B11";
i have a variable called string resultCell. How do I get B11 into resultCell.
it doesn't seem to work for me in c#. I have tried things like ....
resultCell = (string)data;
resultCell = Convert.ToString(data);
More information
I am using excel services to get a value in a cell. the excel services returns a object[]. Its a single element though. I want the value in a string. i.e. B11 in the object as string.
Much of the time, calling data.ToString() will work. Without the specific type of the data object, there isn't any way to answer the question.
Is the value stored in a property of data?
Edit: Could you try (string)object[i], where i is the index of the particular piece of data you want to retrieve? If you need every value in the array, you'll probably want to use a foreach loop.
i think you need to just assign value as data.ToString() to cell.
hope this work for you.