return datareader function error

liunx

Guest
Im a rookie at .net and I am trying to figure out why I keep on getting this one error in a return from this function called fetchReader coming from a fetch reader "<script language="C#" runat="server" src=http://www.webdeveloper.com/forum/archive/index.php/"fetchData_oledb.cs" />"

this is the function on the file (fetchData_oledb.cs):


OleDbDataReader fetchReader ( string query, string db ) {
// connect to data source
OleDbConnection myConn = new OleDbConnection (
"Provider=Microsoft.Jet.OleDb.4.0; Data Source=" +
Server.MapPath ( "~/access_db/" + db + ".mdb" ) );

// initialize command object with query
OleDbCommand myCmd = new OleDbCommand ( query, myConn );

// open connection
myConn.Open ( );

// return datareader
return myCmd.ExecuteReader ( CommandBehavior.CloseConnection );
}



and here is the code returning this function


<script language="C#" runat="server" src=http://www.webdeveloper.com/forum/archive/index.php/"fetchData_oledb.cs" />

<script runat="server">
void Page_Load ( Object src, EventArgs e ) {
// define the query
string query = "SELECT * FROM Employees where EmployeeID='" +
Request.QueryString [ "id" ] + "'";

// fetch reader and bind to a datalist
myData.DataSource = fetchReader ( query, "test" );
myData.DataBind ( );
}
</script>


I am getting this followign error:

ata type mismatch in criteria expression.
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.Data.OleDb.OleDbException: Data type mismatch in criteria expression.

Source Error:

Line 14:
Line 15: // return datareader
Line 16: return myCmd.ExecuteReader ( CommandBehavior.CloseConnection );
Line 17: }
Line 18:Hi, ExecuteReader() actually returns a SqlDataReader data type, not myData.dataSource type.
that's why the compiler complained "data type mismatch"

try this:


// return datareader
SqlDataReader reader = myCmd.ExecuteReader ( CommandBehavior.CloseConnection );
while(myReader.Read()) {
//code for ur dataset goes here (i assume u want return dataset
}
myReader.Close();
return (whatever the name u have for that dataset)


this should do the trick...Yup, sirpelidor is right about the typing. However, you can actually use any object that implements IEnumerable as your datasource. The most common method of providing this is with a datatable:

private const string MYTABLE="mytable";

..
..
myData.DataSource = GetDataSource(sql); // where this function returns a datatable.
myData.DataBind ( );

GetDataSource(string sql){
sqldataadapter da = new sqldataadapter(sql, configurationsettings.appsettings["SQL_CONNECT"]);

da.tablemappings.add("Table",MYTABLE);
dataset ds = new dataset();
da.fill(ds)

return ds.tables[MYTABLE]
}

Apologies for any syntax errors, but I don't know c#
 
Back
Top