简体   繁体   中英

Need convert varchar to numeric and get datagridview current cell value

I have a problem. I have table 'USER' in my DB with(ID[varchar] and PK NOT AI, name[varchar], surname[varchar]) In my c# code I have a datagridview. I display all data from my table 'USER'.

For example:

1234 - John - Smith
1347 - Jack - Russel

After I need click on grid cell, and selected data must be displayed in textboxes. For example I click on 1234 cell and in textBox1 I'll display 'name', in textBox2 - 'lastname'. It makes by ID.

My code is:

connecClass connStr = new connectClass();
DataGridViewRow currentrow = dataGridView1.CurrentRow;
string currStr = currentrow .Cells[0].Value.ToString();
DataTable tableData = connStr.query("SELECT * FROM USER WHERE ID=" +
                                    currentrow);

And I get an error: Error converting type data varchar to numeric After I tried another way, replace.Cells[0] to.Cells["ID"], but get an error: cannot find column ID.

May be you can help me or some other ideas?

You say that the column ID is of type varchar . Maybe try the following:

DataTable tableData = connStr.query("SELECT * FROM USER WHERE ID='" +
                                    currStr + "'");

In other words, put the argument currStr between single quotes (').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM