 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 6th, 2006, 07:15 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 6th, 2006, 07:42 PM
|
|
Wrox Technical Editor
|
|
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 7th, 2006, 02:14 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

March 7th, 2006, 04:25 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 7th, 2006, 02:28 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

March 7th, 2006, 05:56 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

March 7th, 2006, 09:40 PM
|
|
Wrox Technical Editor
|
|
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 7th, 2006, 09:48 PM
|
|
Wrox Technical Editor
|
|
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hmm... What's going on here?
Imar is a doctor.. :p
- A.Kahtava
|
|

March 8th, 2006, 02:09 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
quote:Hmm... What's going on here?
Imar is a doctor.. :p
|
?????
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

March 8th, 2006, 03:03 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I mean he is a PHD postgraduate in ASP.net
|
|
 |