sql query problem with MS Access and C#

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Sep 2004
Posts: 89
Reputation: msaqib is an unknown quantity at this point 
Solved Threads: 1
msaqib msaqib is offline Offline
Junior Poster in Training

sql query problem with MS Access and C#

 
0
  #1
Feb 3rd, 2006
[HTML][/HTML]
using System;
 
using System.Collections;
 
using System.ComponentModel;
 
using System.Data;
 
using System.Data.OleDb;
 
using System.Drawing;
 
using System.Web;
 
using System.Web.SessionState;
 
using System.Web.UI;
 
using System.Web.UI.WebControls;
 
using System.Web.UI.HtmlControls;
 
namespace lms
 
{
 
///<summary>
 
/// Summary description for inc_search_books.
 
///</summary>
 
public class inc_search_books : System.Web.UI.Page
 
{
 
protected System.Web.UI.WebControls.TextBox Q;
 
protected System.Web.UI.WebControls.Button Button1;
 
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
 
protected System.Web.UI.WebControls.Label Label1;
 
protected System.Web.UI.WebControls.DropDownList SearchType;
 
 
 
private void Page_Load(object sender, System.EventArgs e)
 
{
 
// Put user code to initialize the page here
 

 
if (Session["Student"]!="Yes")
 
{
 
Response.Redirect("Login1.aspx"); 
 
}
 
 
 
}
 
#region Web Form Designer generated code
 
override protected void OnInit(EventArgs e)
 
{
 
//
 
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
 
//
 
InitializeComponent();
 
base.OnInit(e);
 
}
 
 
 
///<summary>
 
/// Required method for Designer support - do not modify
 
/// the contents of this method with the code editor.
 
///</summary>
 
private void InitializeComponent()
 
{ 
 
this.Button1.Click += new System.EventHandler(this.Button1_Click);
 
this.Load += new System.EventHandler(this.Page_Load);
 
}
 
#endregion
 
private void Button1_Click(object sender, System.EventArgs e)
 
{
 
if (Q.Text!="")
 
{
 
string source = "";
 
switch (SearchType.SelectedIndex)
 
{
 
case 0:
 
source = "select * from book where name like '*" + Q.Text.ToString() + "*' OR description like '*" + Q.Text.ToString() + "*'";
 
break;
 
case 1:
 
source = "select * from book where isbn like '*" + Q.Text.ToString() + "*'";
 
break;
 
case 2:
 
source = "select * from book where publisher like '*" + Q.Text.ToString() + "*'";
 
break;
 
default:
 
source = "select * from book";
 
break;
 
}
 
Response.Write(source);
 
OleDbConnection con = new OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["MM_CONNECTION_STRING_lms"]);
 
con.Open(); 
 
OleDbCommand cmd=new OleDbCommand(source,con); 
 
OleDbDataReader r = cmd.ExecuteReader();
 
string StrResult="<table width=100% style=\"border:1px solid #ffffff;\">";
 
StrResult += "<tr><td align=center height=35 valign=middle style=\"border:1px solid #ffffff;background-color:#fed500;color:#000000;\"><b>Search Results</b></td></tr>";
 
 
 
while(r.Read()){
 
StrResult += "<tr><td><div class=header><b>" + r["Name"] + "</b> by "+ r["authors"] +"</div><BR>"+r["description"] +"</td></tr>";
 
StrResult += "<tr><td><b>Price: </b>"+ r["price"] +" | ";
 
StrResult += "<b>ISBN: </b>"+ r["isbn"] +" | ";
 
StrResult += "<b>Quantity: </b>"+ r["NoOfAvailableBooks"] +" | ";
 
StrResult += "<b>Publisher: </b>"+ r["publisher"] +" | ";
 
if (r["status"].ToString()=="True")
 
{
 
StrResult += "<b>Available: </b><font color=00ff00>Yes</font>";
 
}
 
else
 
{
 
StrResult += "<b>Available: </b><font color=fed500>No</font>";
 
}
 
if (r["type"].ToString()=="True")
 
{
 
StrResult += " | <b>PDF: </b><font color=00ff00>Yes</font>";
 
}
 
else
 
{
 
StrResult += " | <b>PDF: </b><font color=fed500>No</font>";
 
}
 
StrResult += " | <a href=\"inc_request_book.aspx?BookID="+r ["Id"] +"\">Request to Issue this book</a>";
 
StrResult +="<BR><strong>Accession #: </strong>" +r["Accession"].ToString()+"</td></tr>";
 
 
 
}
 
StrResult += "</table>";
 
r.Close();
 
con.Close();
 
cmd.Dispose();
 
Label1.Text=StrResult;
 
}
 
 
 
}
 
 
 
}
 
}

This is an asp.net web page source code in C#. on the Button1_Click event it searches the data base and returns the records fetched. We create the query on the basis of drop down list box 'SearchType' and the query text from the text box 'Q'. Now when it creates the query that query doesnt return any record. for example for slected index 1 and Q="x" it shows no result from the data base which have the following table and records.

name description isbn publisher
XML XML 454 wrox
Executive Management Executive Management 4545 wrox
Electrical Engineering Electrical Engineering 4545 wrox

query it generates is: select * from book where name like '*x*' OR description like '*x*';

but when i runthis query in MS access database it returns the two records.

Why is it so, any one have an idea. thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: sql query problem with MS Access and C#

 
0
  #2
Feb 5th, 2006
ok below is code i made to connect to a access database.
the Select function takes your query and returns a dataset

datasets are really easy to work with. if you dont have any experience with them i could post how to use them.

also use parameters this doesnt have it, i have some on my webpage that i didnt want to dig up, but it prevents sql injections,

oh yeah and try to keep the sql function outside of the button click because it makes it easier to manage if you ever need to use it again on that page or you could just copy and paste it onto a different page without editing stuff


  1. using System;
  2. using System.Data;
  3. using System.Data.Common;
  4. using System.Data.OleDb;
  5.  
  6. namespace DataConnection
  7. {
  8. /// <summary>
  9. /// Creates connection to query and modify a database
  10. /// </summary>
  11. public class DBConnection
  12. {
  13.  
  14. private System.String strConnection;//connection string
  15.  
  16.  
  17. public DBConnection(string ConnectionString)
  18. {
  19. strConnection = ConnectionString;
  20. //strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
  21. // @"Data Source=\dir\Mydb.mdb; User Id=dbUser; Password=pass";
  22. }
  23.  
  24.  
  25.  
  26. protected OleDbConnection Connect()
  27. {
  28. OleDbConnection conn = null;
  29.  
  30. //create the connection from the connection string
  31. conn = new OleDbConnection(strConnection);
  32.  
  33. conn.Open();
  34.  
  35.  
  36. return conn;
  37. }
  38.  
  39.  
  40. //Used for database SELECT queries
  41. public System.Data.DataSet Select(string selectQuery)
  42. {
  43.  
  44. if(selectQuery.Length <=1)
  45. {
  46. return null;
  47. }
  48.  
  49.  
  50. //create new connection
  51. OleDbConnection conn = this.Connect();
  52.  
  53. DataSet dataSet = new DataSet();
  54.  
  55.  
  56. //create new adapter and command builder
  57. OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
  58. OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
  59.  
  60.  
  61. //Send SELECT query - set the dataadapters select statment to our select
  62. // and out open connection
  63. dataAdapter.SelectCommand = new OleDbCommand (selectQuery, conn);
  64.  
  65.  
  66. //fills our datatable with retreived table
  67. dataAdapter.Fill(dataSet);
  68.  
  69.  
  70. conn.Close();
  71. conn.Dispose();
  72.  
  73. return dataSet;
  74. }//end Select
  75.  
  76.  
  77.  
  78. //Used to INSERT, DELETE, UPDATE sql statments
  79. public void Modify(string mod)
  80. {
  81.  
  82.  
  83. OleDbConnection conn = this.Connect();
  84.  
  85. OleDbCommand command = new OleDbCommand(mod,conn);
  86.  
  87. command.ExecuteNonQuery();
  88.  
  89. conn.Close();
  90. conn.Dispose();
  91.  
  92.  
  93. }//end Modify
  94.  
  95.  
  96. }//end DBConnection
  97. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 89
Reputation: msaqib is an unknown quantity at this point 
Solved Threads: 1
msaqib msaqib is offline Offline
Junior Poster in Training

Re: sql query problem with MS Access and C#

 
0
  #3
Feb 7th, 2006
sorry, actually u didnt understand my problem. I jsut wanted to know that
if i run "select * from book where name like '*x*' OR description like '*x*';" from the MS Access then it returns some records containing X somewhere in the text.
But if i run it throught C# code and execute the query it returns no records.

why?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: sql query problem with MS Access and C#

 
0
  #4
Feb 7th, 2006
oh my bad, well the reason is because '*' is not the wildcard its '%'
so you just new to replace the *'s in your like clauses with %'s
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 89
Reputation: msaqib is an unknown quantity at this point 
Solved Threads: 1
msaqib msaqib is offline Offline
Junior Poster in Training

Re: sql query problem with MS Access and C#

 
0
  #5
Feb 8th, 2006
wel sir, i tried both the * and % . but both are not working even.

Do u think there may be some other problem. Have anybody checked this sort of quiries...

Coz i have tried many queries with like keyword, but all are not working.
Plz any one try it and give me the results.
thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 4
Reputation: colpace is an unknown quantity at this point 
Solved Threads: 0
colpace colpace is offline Offline
Newbie Poster

Re: sql query problem with MS Access and C#

 
0
  #6
Mar 28th, 2006
i have the same problem. i have tried both * and %. * works in MS Access but not from C#.

did u manage to solve it or go around it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: sql query problem with MS Access and C#

 
0
  #7
Mar 28th, 2006
are you even connecting to the database? like can you run a simple "select * from book"

because i just tried "select * from book where name like '%x%' OR description like '%x%'"
in c# and it worked fine



check your connection string maybe, it should look like this for access
  1. string strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Mydb.mdb;";
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 4
Reputation: colpace is an unknown quantity at this point 
Solved Threads: 0
colpace colpace is offline Offline
Newbie Poster

Re: sql query problem with MS Access and C#

 
0
  #8
Mar 29th, 2006
yes i am connecting to the database, and i am getting,inserting and updating data in the db.

However when i run an SQL query which includes the LIKE statement and add the wild card * before and after the text being searched, the returned resultset is empty. If I run the SAME query directly on Access as a query, the result set is not empty.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 89
Reputation: msaqib is an unknown quantity at this point 
Solved Threads: 1
msaqib msaqib is offline Offline
Junior Poster in Training

Re: sql query problem with MS Access and C#

 
0
  #9
Mar 29th, 2006
No i am still having the same problem.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: sql query problem with MS Access and C#

 
0
  #10
Mar 29th, 2006
can i see the latest version of your page and code
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC