Datagrid problem - no results to page?

R

Rob Meade

Hi all,

Haven't really played with these beasts very much - so probably my fault as
to why its not working.

I found an example of some code here -
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx - and then changed
the relevant parts for my application - below is my code, when it runs I
enter some search criteria into a text box - I have run the sql statement
seperately to ensure that it does indeed return results and it does - 1
row - yet when my code runs I get nothing - no errors which is good - but no
data either! Any help with this would be appreciated.

Code:

' Declare variables
Dim strSearchConsultant As String
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand
Dim strSQL As String

strSearchConsultant = txtSearchConsultant.Text

' Create and open our database connection
objConnection = New
SqlConnection(ConfigurationSettings.GetConfig("appSettings")("connStringBris
tolPDD"))
objConnection.Open()

' Create command object
objCommand = New SqlCommand
objCommand.Connection = objConnection

' Generate sql statement
strSQL = "SELECT CrfDHANationalCode, CrfCode, CrfExpansion FROM
viewCDSConsultants "
strSQL += "WHERE UPPER(CrfCode) LIKE '%@ConsultantCode%' OR
UPPER(CrfExpansion) LIKE '%@ConsultantName%'"

' Add parameters
objCommand.Parameters.Add("@ConsultantCode",
strSearchConsultant.ToUpper)
objCommand.Parameters.Add("@ConsultantName",
strSearchConsultant.ToUpper)

' Set our command object
objCommand.CommandText = strSQL
' Set the datagrid's datasource to the datareader and databind
dgSearchConsultantResults.DataSource =
objCommand.ExecuteReader(CommandBehavior.CloseConnection)
dgSearchConsultantResults.DataBind()


Regards

Rob
 
M

Martin Marinov

Hi Rob,

I have 2 questions:
1. When are you binding the datagrid ( in which event )?
2. what is strSearchConsultat ?

the practise with binding object in asp.net is like this:

Page Load event
If not Ispostback then
'bind datagrid
end if

button click event
'bind datagrid based on user's inputs


Regards
Martin
 
R

Rob Meade

...
Hi Rob,

I have 2 questions:
1. When are you binding the datagrid ( in which event )?
2. what is strSearchConsultat ?

the practise with binding object in asp.net is like this:

Page Load event
If not Ispostback then
'bind datagrid
end if

button click event
'bind datagrid based on user's inputs

Hi Martin,

Many thanks for your reply - it turns out my datagrid code was actually ok -
the problem was actually my sql statement!

Apparantly the following isn't any good..

LIKE '%@myParameter%'

neither is

LIKE %@myParameter%

the wilcards have to be part of the parameter - so...

@myParameter = "%fred%"

for example...

Once I'd resolved this it was all ok - thanks for the reply though.

Regards

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top