Issue with Large sql query in asp.net webpages - c#

Hi Hope someone can help.
I am trying to render a list of all my product url's I am using ASP.NET WEBpages (not WebForms or MVC).
But if the Database query is over a certain amount of records it gives me the following error.
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform
runtime binding on a null reference
If I put into the SQL query Top 500 it works fine.
my db query
var db = Database.Open("MyConnectionString");
var Products = "SELECT Top 500* FROM shop_products WHERE site_id = '99' AND product_active = 'Y' ORDER BY product_name ASC";
I used to be able to do this in Classic ASP.
Might there be a limit on SQL query sizes in asp.net, If so how do i get around this.
Help

The key phrase in the error message is "cannot perform runtime binding on a null reference".
I suspect that the top 500 rows in the "shop_products" table did not have a column value with a NULL value. That is why that select worked and the select without the "top 500" qualifier did not.
Your code needs to detect DBnull values and assign a default value for these cases or do some sort of error processing to avoid this runtime error.

To solve this I split this down into smaller SQL query's, split out by the category's the products where in.

Related

SQL Server Report Builder 3 using SQL IN operator with parameter not working

I am attempting to filter a dataset in SSRS using the IN operator on a single column. When I add more than one value it fails.
For simplicity this is my SQL statement excluding other filters to focus on the new problem.
SELECT * FROM vActivityDetailed WHERE Category IN (#Category)
#Category is affected by an override expression under Dataset properties.
=Split(Parameters!Category.Value, ",")
Instead of using a multivalue parameter, I treat this as a single value and hope that by doing a split, the SSRS should execute like this.
C# - ASP.NET Code
string categoryString = "Expenses, Misc, Accounts-Receivable";
paramPayType.Name = "Category";
paramPayType.Values.Add((categoryString!= "" ? categoryString : null));
SSRS Final SQL Execution (Expected)
SELECT * FROM vActivityDetailed WHERE Category IN ('Expenses', 'Misc', 'Accounts-Receivable')
Since I've tested this directly on the SQL using SQL Server Management Studio, I know the above SQL should work since it returns records, but when applied in SSRS it fails with An error has occurred during report processing. (rsProcessingAborted)
The parameter value in SSRS is an array of strings when multiple values are selected. The Split function takes a single string as its argument. You can't use Split with an array. SSRS with SQL Server handles the splitting of the values for you so you should be able to just pass the parameter straight through.
If you need to actually split the values for some reason, I would recommend using a custom SQL function. There are lots of examples if you run a search for that.

Simple LINQ query gets NullReferenceException even though SQL Server table has data

I have a simple Linq query that is failing suddenly. This worked up until yesterday and I have not been able to determine the problem.
The query is simply getting a complete view from SQL Server
using(JobEntities JE = new JobEntities())
{
var BatchData = (from x in JE.vwBatchObjectActiveBatches select x).ToList();
}
Starting yesterday this line gets a
NullReferenceException (Object reference not set to an instance of an object)
My suspicion was that a user put in bad data causing the view to fail on SQL Server, but I have checked SQL Server itself and the view runs fine and populates with data.
This query was running in the middle of a larger function loading data from many places, so I have created a test case where I simply load the main window and run this query directly in the code behind to make sure that nothing else is affecting it, and the query still fails. All other Linq queries that I run in this project work still, only this one fails. The app is not under any production right now, and has been static for several months at least.
When I look at the JE in the watch window I can see the vwBatchObjectActiveBatches and it lists 164 records in the Local section -- this matches the view results on SQL Server. Expanding the Results View shows the null error again.
How can I find and fix whatever is causing this query to fail? Why does the results view show an error but the local Line shows the data that I am attempting to get?
It looks like your database returns NULL where Entity Framework does not expect/allow it. Data returned should be in accordance with the definition of its datamodel objects.
Solution: either 'fix' the data, or fix the query that produces it, or change the definition of your datamodel objects to allow NULL for the conflicting field(s).

Selecting the same column from the same table multiple times in the same statement

I am converting a VB6 app to C# with an SQL Server back end. The app includes a very general query editor that allows the user to write any select query and return the results visually in a grid control. Some of the tables have several hundred columns (poor design, I know but I have no control over this). A typical use case for an admin user would be to
select * from A_Table_With_Many_Columns
However, while they want to be able to view all the data, they are particularly interested in 2 columns and they want these to be displayed as the first 2 columns in the grid (instead of 67th and 99th for example) so instead they execute the following statement:
select First_Interesting_Field, Second_Interesting_Field, *
from A_Table_With_Many_Columns
Then they will go and modify the data in the grid. However, when saving this data, it results in a concurrency violation (DBConcurrencyException). This worked fine with the connected RecordSets of VB6 but not so well in C#. I have tried a myriad of solutions to no avail.
Does anyone know how to handle this exception in a generic way? (Remember, the user can type ANY select statement or join etc. into the query editor)
Does anyone know how I might manipulate the columns returned such that I delete the 2 columns that appear further on in the list? (My difficulty here is that if the column name in the database is EMail so I do select Email, * from Blah the 2 pertinent columns returned are EMail and ADO.NET or C# aliases the second EMail column from the * portion of the query as EMail1 so I am not able to detect the second column as a duplicate and remove it)
Does anyone have an alternate solution I have not thought of?
Thank you very much
Actually, you could rename all variables to something like email_userdefined by doing something like this:
SELECT First_Interesting_Field as First_Interesting_Field_userdefined, Second_Interesting_Field as Second_Interesting_Field_userdefined, *
from A_Table_With_Many_Columns
Replace user_defined with whatever you want, like order number or anything else user acceptable

What to use in this SqlDataAdapter?

I have a SqlDataAdapter that looks something like:
("Select prodID, CatalogType, prodName, catID, Integration, ItemProfileCatalogID From ShoppingCart t1
INNER JOIN ItemCatalogProfile t2 ON t1.CatalogType = t2.CatalogType
WHERE SessionID = '" + Session["ID"] + "' AND CatalogID ='" + Session["Customer"] ....)
there are a few more included in the where statement, but the one I cannot seem to get to work is:
ItemProfileCatalogID ..
I need to include this to narrow down the items down to only one of each and this variable will do just that if I can figure out what to use in this statement.
I've tried
viewstate[""]
Request.QuerryString[""]
Session[""]
and I cant seem to get those to work..
The problem I am having is, the current shopping cart if I do not have this item to filter, it will return every instance of that particular product in the database because there are up to 250 listings of one item for different catalogs, and that is where the ItemProfileCatalogID comes in, that will filter it down to just the one item
Any suggestions?
Thank you
CatalogID is probably numeric and you are using it as a string in your SQL statement.
It could also be that you have some syntax error in your SQL statement not easily detectable with all the string concatenation going on or due to CatalogID containing a naughty character (' for example)
Also, please check out parametrized queries; concatenation when building SQL statements is usually not a very good idea (SQL injection).
As InBetween said it probably is an issue with it being numeric, you should be able to use the .toString() method to solve this.
Also Stored procedures are generaly more advisable to use for a number of reasons, in your case you mentioned you are working with a shopping cart which suggests maybe some kind of ecommerce application? Using a stored procedure here will mean that SQL does not have to compile your select statement every time you run it thus improving performance and having the code in a stored proc will also increase maintainability and possibly allow you to reuse the procedure in other places.
Added to this you are not actually transmitting the select statement over the network which increases security as no one can intercept this and gain insight into your table structures, and as InBetween mentioned above a stored procedure will help you protect against injection attacks or just simply unforseen data as you can add error handling more easily.

Paging in ASP web page?

I'm new to this so forgive me if this is trivial. Anyway, I'm creating a simple ASP web page that reports data from a table in a database that has 5 items in it. I use a GridView to display the data and that works fine, but I'd like to limit the results to three items so that I can allow paging. I tried configuring the select statement from something like "select * from country" to
select * from country limit 3
but I get a "There was an error executing the query" message when I try to test it. Is there some other way that I have to do this?
set Gridview's property PageSize=3
Try to use TOP keyword instead if you use SQL Server database:
SELECT TOP 3 * FROM COUNTRY;
To do this in SQL Server you need to use top
select top 3 * from country
Depending on the total number of records you are expecting to have in the country table a simple solution would be to use the asp:DataPager control without any limits on the number of rows returned from the SQL.
This will handle all the paging for you, however the full data set is retained in the page view state and so is not an appropriate solution is you are expecting 1000's of records.

Categories