Home Ask Login Register

Developers Planet

Your answer is one click away!

CodeMan February 2016

not able to load odbcdatareader to a datatable

I am using an Informix db, and I am trying to get data for a specific item and store it in a datatable.

I checked the following:

1) connection string looks good

2) the connection is able to open

3) I used the same connection string from the web.config on a dataset creating a table adapter and it is able to retrieve the record.

This is the code I am using:

var connectionstring = ConfigurationManager.ConnectionStrings["TestDataTable"].ConnectionString;
OdbcConnection con = new OdbcConnection(connectionstring);
//con.ConnectionString = connectionstring;
if (TxtItem.Text != hold_item)
    OdbcCommand cmd = new OdbcCommand(@"Select t_item,t_idsc,t_upct,
                                        from informix.tsckcm907
                                        where t_item = " + stitem, con); 
    OdbcDataReader myReader = cmd.ExecuteReader();
    DataTable testdt = new DataTable();
    foreach (DataRow row in testdt.Rows)
       lbldesc.Text = row["t_idsc"].ToString();
           DropDownList2.SelectedIndex = 1;
           object stlanguage = 1;
           hold_language = Convert.ToString(stlanguage);
           TxtBestBefore.Text = row["t_best"].ToString();
           holdbest = Convert.ToInt16(TxtBestBefore.Text);

in debug mode my error occurs at the OdbcDataReader line: error message:

An exception of type 'System.Data.Odbc.OdbcException' 
occurred in System.Data.dll but was not handled in user code

Additional information: ERROR [42000] [Informix]
[Informix ODBC Driver][Informix]A syntax error has 


MichaƂ Niklas February 2016

If your Informix ODBC driver says: "A syntax error has occurred" then you have to check your SQL statement:

"Select t_item,...  from informix.tsckcm907 where t_item = " + stitem

I think that something is wrong with stitem. We don't know what type and value it is, but if its type is some kind of string or date then it may be in the wrong form. Easiest way is to extract full SQL statement (simply print it before execution) and use it with some database editor (for example db_access from Informix). Then make it work in SQL editor and transform stitem variable into acceptable form (add quotes, escape internal quotes, escape special characters etc.)

I also recommend use of PreparedStatement that separates your query from data. This way you do not need to worry about stitem form. No quotes, no escaping, just place holder in query string and value added separately.

I don't use C# but I see that C# can work with preapred statements with unnamed parameters:

cmd.CommandText = "SELECT ... FROM ... WHERE t_item = ?";
cmd.Parameters.Add("@t_item", ObdcType.VarChar, 200).Value = t_item;

or with named parameters:

cmd.CommandText = "SELECT ... FROM ... WHERE t_item = @t_item";
cmd.Parameters.Add("@t_item", ObdcType.VarChar, 200).Value = t_item;

I use unnamed parameters from ODBC so Informix driver can work with such parameters but you will have to check it yourself with C#.

Post Status

Asked in February 2016
Viewed 3,284 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life