473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Binding to Gridview

I'm having trouble getting a gridview to bind. I probably missing something
completely obvious and would appreciate any help on offer.

I'm passing parameters via querystring, and have created a stored proc as
follows:

qGetSearchResults:

SELECT
Adverts.AdvertID,
Location.Location,
Jobtitle.JobTitle,
Speciality.Speciality FROM (((Adverts
INNER JOIN Employer ON Adverts.EmployerID = Employer.EmployerID)
INNER JOIN Jobtitle ON Adverts.JobTitleID = Jobtitle.JobTitleID)
INNER JOIN Location ON Employer.LocationID = Location.LocationID)
INNER JOIN Speciality ON Adverts.SpecialityID = Speciality.SpecialityID
WHERE
(@JobTitleID IS NULL OR Adverts.JobTitleID = @JobTitleID)
AND (@JobTitleID IS NULL OR Adverts.JobTitleID = @JobTitleID)
AND (@SpecialityID IS NULL OR Adverts.SpecialityID = @SpecialityID)
AND (@LocationID IS NULL OR Employer.LocationID = @LocationID)
AND (@KeyWords IS NULL OR Adverts.InfoText LIKE '%' + @KeyWords + '%')
AND (((Adverts.StartDate)<=GetDate())
AND (Adverts.EndDate)>=GetDate())
ORDER BY Location.Location

I have verified this works as expected in the Query Designer.

On the page, I have placed a DataSource Control and a GridView. The code
for both is as follows:

<asp:GridView ID="gvSearchResults" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="AdvertID" DataSourceID="dsSearchResults">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="AdvertID" HeaderText="AdvertID"
InsertVisible="False"
ReadOnly="True" SortExpression="AdvertID" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="JobTitle" HeaderText="JobTitle"
SortExpression="JobTitle" />
<asp:BoundField DataField="Speciality" HeaderText="Speciality"
SortExpression="Speciality" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="dsSearchResults" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxx %>"
SelectCommand="qGetSearchResults" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="JobTitleID" QueryStringField="JobTitleID"
Type="Int32" />
<asp:QueryStringParameter Name="SpecialityID"
QueryStringField="SpecialityID" Type="Int32" />
<asp:QueryStringParameter Name="LocationID" QueryStringField="LocationID"
Type="Int32" />
<asp:QueryStringParameter Name="KeyWords" QueryStringField="KeyWords"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>

If I navigate to the page using a URL and querystring like this:

searchResults.aspx?LocationID=&SpecialityID=&KeyWo rds=&JobTitleID=35

the GridView refuses to appear, despite the fact that executing the Stored
Proc in SQL Server gives me 10 results.

What have I missed?
Mike
Oct 7 '06 #1
4 2580
You need to do a DataBind for the control sometime before the page renders.
myDataGrid.DataBind


"Mike" <ne****@dotnet.comwrote in message
news:uS**************@TK2MSFTNGP04.phx.gbl...
I'm having trouble getting a gridview to bind. I probably missing
something completely obvious and would appreciate any help on offer.

I'm passing parameters via querystring, and have created a stored proc as
follows:

qGetSearchResults:

SELECT
Adverts.AdvertID,
Location.Location,
Jobtitle.JobTitle,
Speciality.Speciality FROM (((Adverts
INNER JOIN Employer ON Adverts.EmployerID = Employer.EmployerID)
INNER JOIN Jobtitle ON Adverts.JobTitleID = Jobtitle.JobTitleID)
INNER JOIN Location ON Employer.LocationID = Location.LocationID)
INNER JOIN Speciality ON Adverts.SpecialityID = Speciality.SpecialityID
WHERE
(@JobTitleID IS NULL OR Adverts.JobTitleID = @JobTitleID)
AND (@JobTitleID IS NULL OR Adverts.JobTitleID = @JobTitleID)
AND (@SpecialityID IS NULL OR Adverts.SpecialityID = @SpecialityID)
AND (@LocationID IS NULL OR Employer.LocationID = @LocationID)
AND (@KeyWords IS NULL OR Adverts.InfoText LIKE '%' + @KeyWords + '%')
AND (((Adverts.StartDate)<=GetDate())
AND (Adverts.EndDate)>=GetDate())
ORDER BY Location.Location

I have verified this works as expected in the Query Designer.

On the page, I have placed a DataSource Control and a GridView. The code
for both is as follows:

<asp:GridView ID="gvSearchResults" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="AdvertID" DataSourceID="dsSearchResults">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="AdvertID" HeaderText="AdvertID"
InsertVisible="False"
ReadOnly="True" SortExpression="AdvertID" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="JobTitle" HeaderText="JobTitle"
SortExpression="JobTitle" />
<asp:BoundField DataField="Speciality" HeaderText="Speciality"
SortExpression="Speciality" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="dsSearchResults" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxx %>"
SelectCommand="qGetSearchResults" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="JobTitleID" QueryStringField="JobTitleID"
Type="Int32" />
<asp:QueryStringParameter Name="SpecialityID"
QueryStringField="SpecialityID" Type="Int32" />
<asp:QueryStringParameter Name="LocationID" QueryStringField="LocationID"
Type="Int32" />
<asp:QueryStringParameter Name="KeyWords" QueryStringField="KeyWords"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>

If I navigate to the page using a URL and querystring like this:

searchResults.aspx?LocationID=&SpecialityID=&KeyWo rds=&JobTitleID=35

the GridView refuses to appear, despite the fact that executing the Stored
Proc in SQL Server gives me 10 results.

What have I missed?
Mike

Oct 8 '06 #2
I thought this happened automatically if you plonked a sqlDataSource and a
data control on a page. It does on other pages where I have done so.

Nevertheless, I think there must be something wrong with the stored proc.
So I am doing a switch case for the 15 possible variations of querystring
values and creating 15 procs, then binding the gridview programmatically in
the Page_Load event to the correct proc depending on which values were
passed. That seems to work. I just hope no one asks me to add a fifth
querystring variable....
"Mr Struggler" <lkjhlkjwrote in message
news:O3**************@TK2MSFTNGP03.phx.gbl...
You need to do a DataBind for the control sometime before the page
renders.
myDataGrid.DataBind


"Mike" <ne****@dotnet.comwrote in message
news:uS**************@TK2MSFTNGP04.phx.gbl...
>I'm having trouble getting a gridview to bind. I probably missing
something completely obvious and would appreciate any help on offer.

I'm passing parameters via querystring, and have created a stored proc as
follows:

qGetSearchResults:

SELECT
Adverts.AdvertID,
Location.Location,
Jobtitle.JobTitle,
Speciality.Speciality FROM (((Adverts
INNER JOIN Employer ON Adverts.EmployerID = Employer.EmployerID)
INNER JOIN Jobtitle ON Adverts.JobTitleID = Jobtitle.JobTitleID)
INNER JOIN Location ON Employer.LocationID = Location.LocationID)
INNER JOIN Speciality ON Adverts.SpecialityID = Speciality.SpecialityID
WHERE
(@JobTitleID IS NULL OR Adverts.JobTitleID = @JobTitleID)
AND (@JobTitleID IS NULL OR Adverts.JobTitleID = @JobTitleID)
AND (@SpecialityID IS NULL OR Adverts.SpecialityID = @SpecialityID)
AND (@LocationID IS NULL OR Employer.LocationID = @LocationID)
AND (@KeyWords IS NULL OR Adverts.InfoText LIKE '%' + @KeyWords + '%')
AND (((Adverts.StartDate)<=GetDate())
AND (Adverts.EndDate)>=GetDate())
ORDER BY Location.Location

I have verified this works as expected in the Query Designer.

On the page, I have placed a DataSource Control and a GridView. The code
for both is as follows:

<asp:GridView ID="gvSearchResults" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="AdvertID" DataSourceID="dsSearchResults">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="AdvertID" HeaderText="AdvertID"
InsertVisible="False"
ReadOnly="True" SortExpression="AdvertID" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="JobTitle" HeaderText="JobTitle"
SortExpression="JobTitle" />
<asp:BoundField DataField="Speciality" HeaderText="Speciality"
SortExpression="Speciality" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="dsSearchResults" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxx %>"
SelectCommand="qGetSearchResults" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="JobTitleID" QueryStringField="JobTitleID"
Type="Int32" />
<asp:QueryStringParameter Name="SpecialityID"
QueryStringField="SpecialityID" Type="Int32" />
<asp:QueryStringParameter Name="LocationID" QueryStringField="LocationID"
Type="Int32" />
<asp:QueryStringParameter Name="KeyWords" QueryStringField="KeyWords"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>

If I navigate to the page using a URL and querystring like this:

searchResults.aspx?LocationID=&SpecialityID=&KeyW ords=&JobTitleID=35

the GridView refuses to appear, despite the fact that executing the
Stored Proc in SQL Server gives me 10 results.

What have I missed?
Mike


Oct 8 '06 #3
OK your using 2005, I'm not familar with that yet
"Mike" <ne****@dotnet.comwrote in message
news:eF**************@TK2MSFTNGP04.phx.gbl...
>I thought this happened automatically if you plonked a sqlDataSource and a
data control on a page. It does on other pages where I have done so.

Nevertheless, I think there must be something wrong with the stored proc.
So I am doing a switch case for the 15 possible variations of querystring
values and creating 15 procs, then binding the gridview programmatically
in the Page_Load event to the correct proc depending on which values were
passed. That seems to work. I just hope no one asks me to add a fifth
querystring variable....
"Mr Struggler" <lkjhlkjwrote in message
news:O3**************@TK2MSFTNGP03.phx.gbl...
>You need to do a DataBind for the control sometime before the page
renders.
myDataGrid.DataBind


"Mike" <ne****@dotnet.comwrote in message
news:uS**************@TK2MSFTNGP04.phx.gbl...
>>I'm having trouble getting a gridview to bind. I probably missing
something completely obvious and would appreciate any help on offer.

I'm passing parameters via querystring, and have created a stored proc
as follows:

qGetSearchResults:

SELECT
Adverts.AdvertID,
Location.Location,
Jobtitle.JobTitle,
Speciality.Speciality FROM (((Adverts
INNER JOIN Employer ON Adverts.EmployerID = Employer.EmployerID)
INNER JOIN Jobtitle ON Adverts.JobTitleID = Jobtitle.JobTitleID)
INNER JOIN Location ON Employer.LocationID = Location.LocationID)
INNER JOIN Speciality ON Adverts.SpecialityID = Speciality.SpecialityID
WHERE
(@JobTitleID IS NULL OR Adverts.JobTitleID = @JobTitleID)
AND (@JobTitleID IS NULL OR Adverts.JobTitleID = @JobTitleID)
AND (@SpecialityID IS NULL OR Adverts.SpecialityID = @SpecialityID)
AND (@LocationID IS NULL OR Employer.LocationID = @LocationID)
AND (@KeyWords IS NULL OR Adverts.InfoText LIKE '%' + @KeyWords + '%')
AND (((Adverts.StartDate)<=GetDate())
AND (Adverts.EndDate)>=GetDate())
ORDER BY Location.Location

I have verified this works as expected in the Query Designer.

On the page, I have placed a DataSource Control and a GridView. The
code for both is as follows:

<asp:GridView ID="gvSearchResults" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="AdvertID" DataSourceID="dsSearchResults">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="AdvertID" HeaderText="AdvertID"
InsertVisible="False"
ReadOnly="True" SortExpression="AdvertID" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="JobTitle" HeaderText="JobTitle"
SortExpression="JobTitle" />
<asp:BoundField DataField="Speciality" HeaderText="Speciality"
SortExpression="Speciality" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="dsSearchResults" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxx %>"
SelectCommand="qGetSearchResults" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="JobTitleID"
QueryStringField="JobTitleID" Type="Int32" />
<asp:QueryStringParameter Name="SpecialityID"
QueryStringField="SpecialityID" Type="Int32" />
<asp:QueryStringParameter Name="LocationID"
QueryStringField="LocationID" Type="Int32" />
<asp:QueryStringParameter Name="KeyWords" QueryStringField="KeyWords"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>

If I navigate to the page using a URL and querystring like this:

searchResults.aspx?LocationID=&SpecialityID=&Key Words=&JobTitleID=35

the GridView refuses to appear, despite the fact that executing the
Stored Proc in SQL Server gives me 10 results.

What have I missed?
Mike



Oct 8 '06 #4
I did mention GridView in my OP as opposed to DataGrid, but nevermind :-)
I've found the answer after many frustrating hours.

My requirement is a common one(I believe). I want to give the user the
option to search on any combination of 4 criteria. This means that one or
more of the criteria could well be NULL. Consequently, the parameter value
will be NULL, and I set that as a default value in the stored proc for each
parameter.

The default for the sqlDataSource is that it doesn't bother to do anything
if you leave any parameter as NULL, so you have to go into its properties
and change the CancelSelectOnNullParameter value to false. It's true by
default. When I say it doesn't do anything, I mean nothing at all. It
doesn't even throw an exception, which I would expect it to do if it was not
prepared to handle NULL values.

The configuration wizard allows you to enter default values for the
parameters as wel as their source. It doesn't allow you to supply NULL as a
default. If you type NULL in, it treats it as a string and complains.

I've got 3 books on ASP.NET 2.0 and this is not mentioned in any one of
them, nor could I find it without deep burrowing through google, despite the
fact that I must have read *almost* every article on dynamic search
conditions and the sqlDataSource on the Internet.

I've rewritten the page and the stored proc so many times I'm bored looking
at it, and all the time my original code works perfectly well with that one
amendment to one property.

Now that I know what to google for, I find I'm not the only person to be
driven round the bend on this one:

http://blogs.wdevs.com/colinangusmac...09/08/197.aspx
http://www.danielroot.com/CodeSpeak/...5/Default.aspx
http://sqlblogcasts.com/blogs/tonyro...06/06/792.aspx
http://www.dotnetmonster.com/Uwe/For...ce-not-binding
http://www.mooredynasty.com/Blog/Blo...Period=2006_01

etc, etc.

I nearly threw my hands up and went back to classic ASP, but now I've
invested so much time in this one flaming page, I've got to stick with
learning Dotnet.

Phew. Rant over :-)

Mike

"Mr Struggler" <lkjhlkjwrote in message
news:uQ**************@TK2MSFTNGP05.phx.gbl...
OK your using 2005, I'm not familar with that yet
"Mike" <ne****@dotnet.comwrote in message
news:eF**************@TK2MSFTNGP04.phx.gbl...
>>I thought this happened automatically if you plonked a sqlDataSource and a
data control on a page. It does on other pages where I have done so.

Nevertheless, I think there must be something wrong with the stored proc.
So I am doing a switch case for the 15 possible variations of querystring
values and creating 15 procs, then binding the gridview programmatically
in the Page_Load event to the correct proc depending on which values were
passed. That seems to work. I just hope no one asks me to add a fifth
querystring variable....
"Mr Struggler" <lkjhlkjwrote in message
news:O3**************@TK2MSFTNGP03.phx.gbl...
>>You need to do a DataBind for the control sometime before the page
renders.
myDataGrid.DataBind


"Mike" <ne****@dotnet.comwrote in message
news:uS**************@TK2MSFTNGP04.phx.gbl...
I'm having trouble getting a gridview to bind. I probably missing
something completely obvious and would appreciate any help on offer.

I'm passing parameters via querystring, and have created a stored proc
as follows:

qGetSearchResults:

SELECT
Adverts.AdvertID,
Location.Location,
Jobtitle.JobTitle,
Speciality.Speciality FROM (((Adverts
INNER JOIN Employer ON Adverts.EmployerID = Employer.EmployerID)
INNER JOIN Jobtitle ON Adverts.JobTitleID = Jobtitle.JobTitleID)
INNER JOIN Location ON Employer.LocationID = Location.LocationID)
INNER JOIN Speciality ON Adverts.SpecialityID = Speciality.SpecialityID
WHERE
(@JobTitleID IS NULL OR Adverts.JobTitleID = @JobTitleID)
AND (@JobTitleID IS NULL OR Adverts.JobTitleID = @JobTitleID)
AND (@SpecialityID IS NULL OR Adverts.SpecialityID = @SpecialityID)
AND (@LocationID IS NULL OR Employer.LocationID = @LocationID)
AND (@KeyWords IS NULL OR Adverts.InfoText LIKE '%' + @KeyWords + '%')
AND (((Adverts.StartDate)<=GetDate())
AND (Adverts.EndDate)>=GetDate())
ORDER BY Location.Location

I have verified this works as expected in the Query Designer.

On the page, I have placed a DataSource Control and a GridView. The
code for both is as follows:

<asp:GridView ID="gvSearchResults" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="AdvertID" DataSourceID="dsSearchResults">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="AdvertID" HeaderText="AdvertID"
InsertVisible="False"
ReadOnly="True" SortExpression="AdvertID" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="JobTitle" HeaderText="JobTitle"
SortExpression="JobTitle" />
<asp:BoundField DataField="Speciality" HeaderText="Speciality"
SortExpression="Speciality" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="dsSearchResults" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxx %>"
SelectCommand="qGetSearchResults" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="JobTitleID"
QueryStringField="JobTitleID" Type="Int32" />
<asp:QueryStringParameter Name="SpecialityID"
QueryStringField="SpecialityID" Type="Int32" />
<asp:QueryStringParameter Name="LocationID"
QueryStringField="LocationID" Type="Int32" />
<asp:QueryStringParameter Name="KeyWords" QueryStringField="KeyWords"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>

If I navigate to the page using a URL and querystring like this:

searchResults.aspx?LocationID=&SpecialityID=&Ke yWords=&JobTitleID=35

the GridView refuses to appear, despite the fact that executing the
Stored Proc in SQL Server gives me 10 results.

What have I missed?
Mike



Oct 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sck10 | last post by:
Hello, I have a GridView that is using the following to connect to a SQL Server 2000 stored procedure: <asp:SqlDataSource ID="dsWebDocList" SelectCommand="sp_web_WebDocument"...
3
by: Hans Merkl | last post by:
Hi, I was wondering if it's possible to bind the header text of a GridView column to a method of an object I have. At the moment I am setting the header texts in Page_Load but I was wondering if...
1
by: Smash | last post by:
I have problem binding gridview.... At first i didn't want to bind gridview at first page load...so in my objectdatasource_selecting event i wrote this: If Not Page.IsPostBack Then e.Cancel =...
3
by: mateo | last post by:
Hello, I have a GridView inside which i want to show 3 dropdownList (one for each Column). So i've created 3 TemplateField Continent Country City
5
by: Amit | last post by:
Hello, I have a simple search screen, with two drop-downs and a text box. There's also a GridView control that is using a SqlDataSource control to show the matching results. The SqlDataSource uses...
8
by: AG | last post by:
ASP.NET 2.0, VS 2005 I have a gridview with paging enabled, bound to an objectdatasource. Both were dropped on to the page and configured via the wizards. Basically working as expected. The...
3
by: RobertTheProgrammer | last post by:
Hi folks, I've got another problem. Basically, I'm trying to use a nested GridView, however the nexted GridView displays no values (even though in debug I'm getting valid values into my DataSet. ...
0
by: Sobin Thomas | last post by:
Hi All, How can I bind the Gridview control to Sql Datasource control on a button click(I see majority of the articles binding datasource at page load) I need to enable the paging and sorting of...
1
by: Sobin Thomas | last post by:
Hi All, How can I bind the Gridview control to Sql Datasource control on a button click(I see majority of the articles binding datasource at page load) I need to enable the paging and sorting of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.