c# - not able to load odbcdatareader to a datatable -
i using informix db, , trying data specific item , store in datatable.
i checked following:
1) connection string looks good
2) connection able open
3) used same connection string web.config on dataset creating table adapter , able retrieve record.
this code using:
var connectionstring = configurationmanager.connectionstrings["testdatatable"].connectionstring; odbcconnection con = new odbcconnection(connectionstring); //con.connectionstring = connectionstring; if (txtitem.text != hold_item) { con.open(); odbccommand cmd = new odbccommand(@"select t_item,t_idsc,t_upct, t_item_upc,t_ctyp,t_citg, t_best,t_disp,t_mold,t_csel informix.tsckcm907 t_item = " + stitem, con); odbcdatareader myreader = cmd.executereader(); datatable testdt = new datatable(); testdt.load(myreader); foreach (datarow row in testdt.rows) { lbldesc.text = row["t_idsc"].tostring(); spanish_item(); { dropdownlist2.selectedindex = 1; object stlanguage = 1; hold_language = convert.tostring(stlanguage); txtbestbefore.text = row["t_best"].tostring(); holdbest = convert.toint16(txtbestbefore.text); } } myreader.close(); myreader.dispose(); cmd.dispose(); con.close(); con.dispose(); }
in debug mode error occurs @ odbcdatareader line: error message:
an exception of type 'system.data.odbc.odbcexception' occurred in system.data.dll not handled in user code additional information: error [42000] [informix] [informix odbc driver][informix]a syntax error has occurred.
if informix odbc driver says: "a syntax error has occurred" have check sql statement:
"select t_item,... informix.tsckcm907 t_item = " + stitem
i think wrong stitem
. don't know type , value is, if type kind of string or date may in wrong form. easiest way extract full sql statement (simply print before execution) , use database editor (for example db_access
informix). make work in sql editor , transform stitem
variable acceptable form (add quotes, escape internal quotes, escape special characters etc.)
i recommend use of preparedstatement separates query data. way not need worry stitem
form. no quotes, no escaping, place holder in query string , value added separately.
i don't use c# see c# can work preapred statements unnamed parameters:
cmd.commandtext = "select ... ... t_item = ?"; cmd.parameters.add("@t_item", obdctype.varchar, 200).value = t_item;
or named parameters:
cmd.commandtext = "select ... ... t_item = @t_item"; cmd.parameters.add("@t_item", obdctype.varchar, 200).value = t_item;
i use unnamed parameters odbc informix driver can work such parameters have check c#.
Comments
Post a Comment