 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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
|
|
|
|

November 20th, 2003, 09:11 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Using DataReader()
Hi Guys,
I want to display the results of this reader object to be displayed in my datagrid. When I execute nothing happens?? What am I doing wrong: -
String strAuthor = Author.Text;
String strTitle = Title.Text;
String strSearch = String.Format("SELECT * FROM Computing WHERE BookAuthor LIKE '{0}' OR BookName LIKE '{1}'", strAuthor, strTitle);
OleDbCommand objCommand = new OleDbCommand();
OleDbDataReader objDataReader = null;
Connect();
objCommand.Connection = objConnection;
objCommand.CommandType = CommandType.Text;
objCommand.CommandText = strSearch;
objDataReader = objCommand.ExecuteReader();
if (objDataReader.Read())
{
dgComputing.DataSource = objDataReader();;
dgComputing.DataBind();
}
objDataReader.Close();
Disconnect();
Adz - Portsmouth Massive
__________________
Adz - Learning The J2EE Ways.
|
|

November 20th, 2003, 12:43 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Are you sure you are running a query that returns data? With the query you have, the values have to match exactly otherwise you won't get data. If you want true search type functionality, you should encase the strings in % to specify wildcards on either side. Of course, this might be dependent on what DB platform you are using.
String strSearch = String.Format("SELECT * FROM Computing WHERE BookAuthor LIKE '%{0}%' OR BookName LIKE '%{1}%'", strAuthor, strTitle);
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

November 20th, 2003, 03:51 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Peter,
after solving one problem, I have another, Once the data is checked and is in the database I want it to be displayed on a datagrid. Is this not possible by placeing the code for binding to a datagrid inside objDataReader.Read() {..DataGrid Code};
Here is my code: -
private void Click_Button(object sender, System.EventArgs e)
{
String strConnection = "Provider=Microsoft.Jet.OleDb.4.0; data source = D:\\ineedabook\\Books.mdb;";
OleDbConnection objConnection = new OleDbConnection(strConnection);
String strSearch = String.Format("SELECT * FROM Computing WHERE BookAuthor LIKE '{0}' OR BookName LIKE '{1}'", TextBox1.Text, TextBox2.Text);
OleDbCommand objCommand = new OleDbCommand();
OleDbDataReader objDataReader = null;
objConnection.Open();
objCommand.Connection = objConnection;
objCommand.CommandType = CommandType.Text;
objCommand.CommandText = "Select * FROM Computing Where BookAuthor = ? OR BookName = ?";
objCommand.Parameters.Add("BookAuthor", OleDbType.VarChar, 30, "BookAuthor");
objCommand.Parameters.Add("BookName", OleDbType.VarChar, 30, "BookName");
objCommand.Parameters["BookAuthor"].Value = TextBox1.Text;
objCommand.Parameters["BookName"].Value = TextBox2.Text;
objDataReader = objCommand.ExecuteReader();
if (objDataReader.Read())
{
DataGrid1.DataSource = objDataReader;
DataGrid1.DataBind();
//Response.Write("I have found a record with that criteria");
}
Any ideas
Adz - Portsmouth Massive
|
|

November 20th, 2003, 04:07 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
That should work. You have the right code sequence there. You set the datasource, then you DataBind(). Can't imagine why this isn't working for you. You have confirmed that the query you are running does really return data?
And the datagrid isn't marked as visible=false or anything like that, right?
How's the datagrid set up? Is it set to autogenerate columns? Post the markup for the datagrid so we can check that out.
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

November 20th, 2003, 06:03 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Its strange,
there is data being returned from the query. Its funny because when I remove the
if(DataReader.Read()){} out it shows the data grid, but when I include the if statement it does not work. Here is my code: -
private void Click_Button(object sender, System.EventArgs e)
{
String strConnection = "Provider=Microsoft.Jet.OleDb.4.0; data source = D:\\ineedabook\\Books.mdb;";
OleDbConnection objConnection = new OleDbConnection(strConnection);
String strSearch = String.Format("SELECT * FROM Computing WHERE BookAuthor LIKE '{0}' OR BookName LIKE '{1}'", TextBox1.Text, TextBox2.Text);
OleDbCommand objCommand = new OleDbCommand();
OleDbDataReader objDataReader = null;
objConnection.Open();
objCommand.Connection = objConnection;
objCommand.CommandType = CommandType.Text;
objCommand.CommandText = "Select * FROM Computing Where BookAuthor = ? OR BookName = ?";
objCommand.Parameters.Add("BookAuthor", OleDbType.VarChar, 30, "BookAuthor");
objCommand.Parameters.Add("BookName", OleDbType.VarChar, 30, "BookName");
objCommand.Parameters["BookAuthor"].Value = TextBox1.Text;
objCommand.Parameters["BookName"].Value = TextBox2.Text;
objDataReader = objCommand.ExecuteReader();
if (objDataReader.Read())
{
DataGrid1.DataSource = objDataReader;
DataGrid1.DataBind();
//Response.Write("I have found a record with that criteria");
}
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button id="Button1" onclick="Click_Button" runat="server" Text="Submit"></asp:Button>
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</p>
</form>
</body>
</html>
Adz - Portsmouth Massive
|
|

November 20th, 2003, 06:23 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Actually, this makes sense. Remove the if. When you bind the reader to the grid, the grid knows how to deal with a reader with no rows. Behind the scenes it's using the IEnumerator interface to basicall "for each" thru the reader. When there are no rows, it knows not to do anything. So just do this:
objDataReader = objCommand.ExecuteReader();
DataGrid1.DataSource = objDataReader;
DataGrid1.DataBind();
|
|

November 21st, 2003, 06:57 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok thanks Peter will do, do you know of any goood sites where I can get god information about the behind the scences IEnumerator Interface?
Adz - Portsmouth Massive
|
|

November 21st, 2003, 07:02 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
MSDN always has the most comprehensive information, if you can get through the technical weight of it.
http://msdn.microsoft.com/library/de...classtopic.asp
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

November 21st, 2003, 07:09 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Peter if for example the serach results in nothing, is there a way of telling the user this. I thought that by using the if(objDataReader.Read()) was true then display the datagrid else display a message stating that nothing exists.
Any ideas??
Adz - Portsmouth Massive
|
|

November 21st, 2003, 10:11 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
OleDbDataReader.HasRows returns True if there is 1 or more rows.
From MSDN article:
Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework
Peter
------------------------------------------------------
Work smarter, not harder.
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| datareader |
MunishBhatia |
ASP.NET 2.0 Professional |
2 |
October 17th, 2007 07:05 AM |
| regarding datareader |
adityamadisetty |
VB.NET |
1 |
May 8th, 2006 09:31 AM |
| DataReader |
truongnnhat |
ASP.NET 1.0 and 1.1 Basics |
2 |
February 18th, 2005 12:41 AM |
| datareader |
surapongmax |
ADO.NET |
1 |
October 8th, 2004 07:53 AM |
| DataReader |
cjcd |
BOOK: Beginning ASP.NET 1.0 |
2 |
March 21st, 2004 01:06 PM |
|
 |