Showing posts with label sqldatasource. Show all posts
Showing posts with label sqldatasource. Show all posts

Saturday, February 25, 2012

AccessViolationException from SQLDataSource

I'n getting a randomAccessViolationException during the select operation of an SqlDataSource (ASP.NET 2.x), after I have modified the CommandText from it'sSelecting event.

I know it's considered a bad idea to dynamically modify the SQL rather than use parameters, but in this instance, I'm more concerned as to why this exception is occurring. It *only* happens if I modify the CommandText and even then I'd say it only occurs 10% of the time. More annoying than anything is that it is returning the correct results. I can catch the exception from theSelected event and the stack trace reveals that the failing method isSQLExecDirectW.

There is nothing wrong with the CommandText syntax and MS documentation states that it can be changed from the Selecting event, so I'm at odds as to why this is happening.

For completeness, the reason I can't use parameters is that I'm using the ODBC Datasource with a Paradox database (Intersolv drivers). I need an optional predicate and IsNull() is not supported, and I cannot get the {fn IFNULL(?, Table.`Field`)} escape sequence to work with an SQL input parameter.

Can anyone put me out of my misery?

This is an exception throwned from unmanaged code because something is trying to access protected memory so it sounds like the database drivers.

Have you checked for updated drivers ? You should probably report the bug to the people providing these drivers.

Friday, February 24, 2012

Accessing value from SqlDataSource

I have a SqlDataSource that returns a list of companies and their details by ProductID. It also returns the name of the product associated with the ProductID as the final column (which means it appears for every record returned). I already have a way of determining how many rows were returned, and use that information in a label to say "Your search has returnedx records".

protected void dsGetSuppliersByProduct_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
int RecordCount = e.AffectedRows;
if (RecordCount == 0)
{ lblRecordCount.Text = "<p>No Records found"; }
else
{
if (RecordCount == 1)
{ lblRecordCount.Text = "<p>Your search returned 1 record"; }
else
{ lblRecordCount.Text = "<p>Your search returned " + RecordCount + " records"; }
}
string ProductName;
}

How can I access the ProductName value so that I can extend the label text to say "Your search has returnedx records for <ProductName>" ?

I found a way to do what I wanted. I changed the label to a literal control (for display purposes) and accessed the ProductName value in the RowDataBound event. I then applied it to another literal. My first go resulted in the ProductName appearing as many times as there were rows, so I checked to see if the Literal.Text had already been assigned. So now my code looks like this:

protected void dsGetSuppliersByProduct_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
int RecordCount = e.AffectedRows;
if (RecordCount == 0)
{ ltRecordCount.Text = "<p>No Records found"; }
else
{
if (RecordCount == 1)
{ ltRecordCount.Text = "<p>1 supplier"; }
else
{ ltRecordCount.Text = "<p>" + RecordCount + " suppliers"; }

}

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string pName = DataBinder.Eval(e.Row.DataItem, "ProductName").ToString();
if (ltProductName.Text == "")
{
ltProductName.Text = " of <strong>" + pName + "</strong></p>";
}
}
}