Search This Blog

Friday, December 4, 2009

Weird Results with datatable.select

Ok,

Was working on a project today and need to get information from the database based off of an already completed query. I first did it by making a routine to run the query and build the select with a customer where statement, but hitting the db 300-400 times per incident was being slow and ineffective. So, I decided to just cut that down to one query and use a select on the resulting datatable.

I would get the results in a datatable, and make my expression.

My criteria was for the account, which was a number, and SQL syntax would be account = 1234567

That kind of worked, but would throw this error...

Min (274) must be less than or equal to max (-1) in a Range object.


After searching Google, I came across this post, Simillar error.

That post said to single quote the qualifier. So, I rewrote the expression to be account = '1234567' and it works.

Tuesday, December 1, 2009

Custom Link Buttons In Gridview

There have been times in my job, where a customized link button in a gridview was needed. While there are probably several ways to do this, the following is the best way to do it that I have found. In my case, I needed the link button to change text, and also change command name, based off of data from a query.

What I did was when creating the binding of the datasource to the gridview in codebehind, I added a datakey that I wanted to key off of.

gv.DataKeyNames = new string[] { "excluded" };


Then in the RowDataBound event, I have the following code within it. Basically it looks at the value I'm keying off of, and if it is a 1, then does one routine, else it does another. In the routine, it gets the LinkButton control from the cell, and then sets the text, forecolor, and command name.

if (Convert.ToString(gvEvents.DataKeys[e.Row.RowIndex].Values["excluded"].ToString()) == "1")
{
LinkButton lb = (LinkButton)e.Row.Cells[10].Controls[0];
lb.Text = "Include";
lb.ForeColor = System.Drawing.Color.Black;
lb.CommandName = "include";
}
else
{
LinkButton lb = (LinkButton)e.Row.Cells[10].Controls[0];
lb.Text = "Exclude";
lb.ForeColor = System.Drawing.Color.Black;
lb.CommandName = "exclude";
}