如何解决错误`你指定了一个无效的列序数 [英] How to solve error `You have specified an invalid column ordinal`

查看:1482
本文介绍了如何解决错误`你指定了一个无效的列序数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:


您指定的列序号无效

You have specified an invalid column ordinal

我已经检查了我的数据库中的列号,我知道这是正确的。这里是我的代码:

I already checked the column number in my database and I know it's right. Here's my code:

using (MySqlCommand cmd = new MySqlCommand(query, con))
{

    cmd.Parameters.AddWithValue("@ID", txtboxID.Text);
    MySqlDataReader dr = cmd.ExecuteReader();

    while(dr.Read())
    {
        int size = dr.GetInt32(3);
        int quantity = dr.GetInt32(4);
        string variant = dr.GetString(2);

        DataGridViewRow row = dataGridView2.Rows
           .Cast<DataGridViewRow>()
           .Where(r => (r.Cells["variant_name"].Value.ToString().Equals(variant) && r.Cells["size"].Value.Equals(size)))
           .First();

        row.Cells["quantity"].Value = quantity;
    }
}

我收到此行上的错误 - > int size = dr.GetInt32(3);

I'm getting the error on this line -> int size = dr.GetInt32(3);

推荐答案

返回三列,列顺序为0,因此您的序数将为0,1和2,如下所示:

Your query is returning three columns, and the column ordinal is 0 based, so your ordinals would be 0, 1, and 2, like this:

int size = dr.GetInt32(1);
int quantity = dr.GetInt32(2);
string variant = dr.GetString(0);

当您的查询返回 variant_name size 数量的顺序(0,1和2)。

As your query returns variant_name, size and quantity in that order (0, 1, and 2).

这篇关于如何解决错误`你指定了一个无效的列序数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆