Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 6th, 2006, 07:15 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to search in database

  hi folks:

I have a little problem, it could be big i don't know!! i want to enter say a name of a department manager in a text box and get all data that is related to that manager. however, i always get the following error message when a type it the name and press the button:

 Server Error in '/ECBS' Application.
--------------------------------------------------------------------------------


The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'System.Data.DataSet' could not be found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'System.Data.DataSet' could not be found.

Source Error:

Line 52: Dim Search As String = TextBox1.Text
Line 53: GridView1.DataSourceID = FindDept(Search).ToString
Line 54: GridView1.DataBind()
Line 55: End Sub
Line 56:



what i did:

i dragged a data grid and configured the datasource manually in the code as you will see in the bottom. i also dragged a text box and a button. And a finally i have used the following code:

Imports System.Data

Imports System.Data.SqlClient

Partial Class Agency_search_page

Inherits System.Web.UI.Page

Dim objcmd As SqlCommand, objconn As SqlConnection

---------------------------------------------------------------------------------------------------------------------------------------

Protected Sub Page_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Make a connection to the database

objconn = New SqlConnection(ConfigurationManager.ConnectionStrin gs("connectionstring1").ConnectionString)

' If page is not post backed, then display the whole datagrid

If Not IsPostBack Then

BindGrid()

End If

End Sub

---------------------------------------------------------------------------------------------------------------------------------------

Sub BindGrid()

objcmd = New SqlCommand("Select * from department ", objconn)

objconn.Open()

GridView1.DataSource = objcmd.ExecuteReader()

GridView1.DataBind()

objconn.Close()

End Sub

---------------------------------------------------------------------------------------------------------------------------------------

Function FindDept(ByVal Dept As String) As System.Data.DataSet

Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(ConfigurationM anager.ConnectionStrings("connectionstring1").Conn ectionString)

Dim queryString As String = "SELECT * from department"

Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand()

dbCommand.CommandText = queryString

dbCommand.Connection = dbConnection

Dim dbParam_DeptName As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter()

dbParam_DeptName.ParameterName = "@depaManagerName"

dbParam_DeptName.Value = Dept

dbParam_DeptName.DbType = System.Data.DbType.String

dbCommand.Parameters.Add(dbParam_DeptName)

Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter()

dataAdapter.SelectCommand = dbCommand

Dim dataSet As System.Data.DataSet = New System.Data.DataSet

dataAdapter.Fill(dataSet)

Return dataSet

End Function

---------------------------------------------------------------------------------------------------------------------------------------

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Search As String = TextBox1.Text

GridView1.DataSourceId= FindDept(Search).ToString

GridView1.DataBind()

End Sub


End Class

the problem is always that gridview1.databind(). Why is that ????

someone will sat why don't use the datasource control from the toolbox. i basically tried that option but i prefered to user datasource manually.



I really need this if someone knows please let me know. Thanx in advance


  #2 (permalink)  
Old March 6th, 2006, 07:42 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your problem seems fairly simple:
"I want to enter say a name of a department manager in a text box and get all data that is related to that manager."

Have you considered using a SQL SELECT statement to get your desired records?

For example: "SELECT * FROM department WHERE depaManagerName = 'TextBoxGeneratedValue' ;"

- A.Kahtava
  #3 (permalink)  
Old March 7th, 2006, 02:14 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You can choose between the data source controls from the toolbox (which are great, and very easy to use), or your own data retrieval.

In the first case, you need DataSourceId, in the latter only DataSource (since you're not pointing to the Id of a Data Control, but to the actual data). So, simply drop the ID from your code:

Dim Search As String = TextBox1.Text
GridView1.DataSource = FindDept(Search).ToString
GridView1.DataBind()

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #4 (permalink)  
Old March 7th, 2006, 04:25 AM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, first of all, thank you very much for the help.
i have basically tried what you have told me Imar before i used datasourceID but still didn't work. (you know sometimes when things don't work properly you just start fiddling and missing around). so far i have used these to commands and none has worked and both sends the same error message.
first one:
GridView1.DataSource = FindDept(Search)
GridView1.DataBind()

and second one:
gridview1.datasourceid= finddept(search).tostring
gridview1.databind():)

please can anyone help


  #5 (permalink)  
Old March 7th, 2006, 02:28 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Are you sure you no longer have the DataSourceId attribute setup in the markup of the page?

This should work:

GridView1.DataSource = FindDept(Search)
GridView1.DataBind()

provided that FindDept returns a DataSet....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #6 (permalink)  
Old March 7th, 2006, 05:56 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi saif44,

Thank you for your personal message. But would you mind asking your questions and posting follow ups here on this forum? Thanks.

Why are you expecting the data to be filtered? Inside FindDept you have a SELECT * FROM department" query. That query is set up to return all rows, regardless of the number of parameters you manually add to the command object. You also need to add a WHERE clause to limit the list...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #7 (permalink)  
Old March 7th, 2006, 09:40 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Based on the data source controls from the toolbox method that Imar has been working on:

As Imar said, you'll need a WHERE clause in your SQL or a filter, or you data won't be filtered.

I'll proceed with the WHERE clause method, your function FindDept should look something like this:

Function FindDept(ByVal Dept As String) As System.Data.DataSet

    Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(ConfigurationM anager.ConnectionStrings("connectionstring1").Conn ectionString)

    Dim queryString As String = "SELECT * from department WHERE depaManagerName = '" + Dept + "';";

    'The rest of existing function code goes here

- A.Kahtava
  #8 (permalink)  
Old March 7th, 2006, 09:48 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmm... What's going on here?

Imar is a doctor.. :p

- A.Kahtava
  #9 (permalink)  
Old March 8th, 2006, 02:09 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
quote:Hmm... What's going on here?

Imar is a doctor.. :p
?????

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #10 (permalink)  
Old March 8th, 2006, 03:03 AM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I mean he is a PHD postgraduate in ASP.net

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search in Database????? Samora C# 2 October 10th, 2007 07:41 AM
need help, to Search from database vikky17 Classic ASP Basics 1 February 5th, 2006 12:35 AM
database search rajuru PHP Databases 2 November 9th, 2004 04:32 PM
Search Database bspradeep Classic ASP Databases 1 September 15th, 2004 05:47 AM



All times are GMT -4. The time now is 09:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.