string location = Environment.CurrentDirectory;
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + location + "\\harita.accdb";
            // get records from the Shippers table
            string commandString = "Select x from map";


            //create the data set command object and the DataSet
            OleDbDataAdapter DataAdapter = new OleDbDataAdapter(commandString, connectionString);

            DataSet ds = new DataSet();
            // fill the data set object
            DataAdapter.Fill(ds, "map");
            // Get the one table from the DataSet
            DataTable dataTable = ds.Tables[0];
            // for each row in the table, display the info
            foreach (DataRow dataRow in dataTable.Rows)
            {
                // listBox1.Items.Add(dataRow["x"]);
                tb.Text = Convert.ToString(dataRow["x"]);
                tbb.Text = Convert.ToString(dataRow["y"]);


                #region Draw Icon
                if (drawicon)
                {
                    double lon = Convert.ToDouble(tb.Text);
                    tb.Text = Convert.ToString(dataRow["x"]);

                    double lat = Convert.ToDouble(tbb.Text);



                    e.Graphics.DrawImage((Image)(harita1.Properties.Resources.icon), new Rectangle((int)(lon), (int)(lat), 23, 23));


                }


                #endregion

            }

Dani AI

Generated

Quick diagnosis for (and as hinted): the usual causes are (a) the result set does not contain a column named "y" so dataRow["y"] is invalid, (b) the "y" field is DBNull, or (c) the value is non‑numeric so your conversion/parsing fails. If the column really isn’t in the DataTable you will see an exception like "Column 'y' does not belong to table." Use the debugger to check dataRow.Table.Columns.Contains("y") and inspect dataRow["y"] or dataRow.IsNull("y") in QuickWatch.

Practical steps to fix and harden the code:

  • Change the query so both columns are returned (verify the table actually has a y column).
  • Check for DBNull before converting, and use typed accessors or TryParse to avoid exceptions.
  • Avoid doing database reads inside a Paint handler; load the data once (Form_Load or background task), keep a list of points, then draw from that list in Paint. Also note that looping through all rows and setting tb.Text/tbb.Text will leave the textboxes with the last row’s values — break or pick the row you intend to edit.

Safe example pattern (new code, not the snippet already posted):

// load both columns, then safely read and parse
string cmd = "SELECT x, y FROM map";
var ds = new DataSet();
// fill ds with adapter...
var table = ds.Tables[0];
foreach (DataRow r in table.Rows) {
    if (r.IsNull("x") || r.IsNull("y")) continue;
    double lon, lat;
    if (!double.TryParse(r["x"].ToString(), NumberStyles.Float, CultureInfo.InvariantCulture, out lon)) continue;
    if (!double.TryParse(r["y"].ToString(), NumberStyles.Float, CultureInfo.InvariantCulture, out lat)) continue;
    tb.Text = lon.ToString(CultureInfo.InvariantCulture);
    tbb.Text = lat.ToString(CultureInfo.InvariantCulture);
    // store lon/lat in a list and call Invalidate() for drawing
    break; // remove if you intend to process all rows
}

Extra checks: confirm tbb really references the expected TextBox (no naming mixup), ensure ReadOnly/Enabled aren’t preventing edits, and if you update controls from a background thread use Invoke.

What is the value of dataRow["y"] when you look at it in the debugger?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.