Click to See Complete Forum and Search --> : [RESOLVED] C# equivalent to PHP/MySQL "mysql_error"?


Nightcat
03-16-2009, 08:56 AM
I have a script that connects to a db called "cards_db.mbd"

but when I run it, nothing happens ^(

although on the page does not return any errors now :confused:



<%@ Page Language="c#" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

void PageLoad()
{

OleDbConnection conn = new OleDbConnection() ; //Build connection object

string connectionStr;
connectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
connectionStr += "Data Source= \"cards_db.mdb\"";
conn.ConnectionString = connectionStr;


//set up a query
string queryString = "select all from cards_table where card_type=\"recognize\"";

OleDbCommand dbCommand = new OleDbCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = conn; //Build command object


conn.Open(); //Connection is open


OleDbDataReader cards_dataReader = dbCommand.ExecuteReader(); //This constructs the DataReader and fills it in

// Bind the dataReader to the datagrid dg1 //
dg1.DataSource = cards_dataReader;
dg1.DataBind();
cards_dataReader.Close();

while(cards_dataReader.Read()){

string card_id=Convert.ToString(cards_dataReader["card_id"]);
string link_to_image=Convert.ToString(cards_dataReader["link_to_image"]);

card.Text = card_id;
}

}

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<form runat="server">
<asp:DataGrid id="dg1" runat="server"></asp:DataGrid>

<asp:Label id="card" runat="server"></asp:Label>
</form>

</body>
</html>

ryanbutler
03-17-2009, 04:44 PM
Three things:

1) string queryString = "select all from cards_table where card_type=\"recognize\"";

I don't think that's a valid query. You're escaping when you don't have to in .NET. Take out the back slashes so that it looks like this:

string queryString = "select all from cards_table where card_type=recognize";


2) You shouldn't need to use a while loop. When you data-bind the data to the data grid, the data grid takes care of the rest.

3) You shouldn't need to escape the location of the database either, just use:

string connectionStr;
connectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
connectionStr += "Data Source= "cards_db.mdb";

Nightcat
03-21-2009, 09:48 AM
It seems that I have made some spelling error in the code as everything worked fine once I copied some code from the other page I have in my stash.