简体   繁体   English

ArgumentOutOfRangeException-索引超出范围。 必须为非负数且小于集合的大小

[英]ArgumentOutOfRangeException - index was out of range. Must be non-negative and less than the size of the collection

Error : ArgumentOutOfRangeException -- index was out of range. 错误:ArgumentOutOfRangeException-索引超出范围。 Must be non-negative and less than the size of the collection 必须为非负数且小于集合的大小

Scenario: A desktop application that reads data from the SQL Server database. 场景:一个桌面应用程序,它从SQL Server数据库中读取数据。 When I want to click on the button, it will open the form and read the data from the database and automatically put it in the Data Grid View, but this will I get an exception.I put the code down 当我想单击按钮时,它将打开表单并从数据库中读取数据,然后将其自动放入“数据网格视图”中,但这会出现异常。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BashgaheVarzeshiI2
{
    public partial class frmuser : Form
    {
        public frmuser()
        {
            InitializeComponent();
        }

        SqlConnection con = new SqlConnection("Data source=(local);initial catalog=BashgahDB; integrated security = true");
        SqlCommand cmd = new SqlCommand();

        void Display()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from Karbar";
            adp.Fill(ds, "Karbar");
            dgvUser.DataSource = ds;
            dgvUser.DataMember = "Karbar";

            dgvUser.Columns[0].HeaderText = "code";
            dgvUser.Columns[1].HeaderText = "username";
            dgvUser.Columns[2].HeaderText = "password";

            dgvUser.Columns[0].Width = 50;
        }
        private void bunifuImageButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bunifuImageButton2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void bunifuImageButton6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bunifuImageButton3_Click(object sender, EventArgs e)
        {
            try
            {
                cmd.Parameters.Clear();
                cmd.Connection = con;
                cmd.CommandText = "insert into Karbar(UName,Password)values(@Uname,@Password)";
                cmd.Parameters.AddWithValue("@Uname", bunifuMaterialTextbox1.Text);
                cmd.Parameters.AddWithValue("@Password", bunifuMaterialTextbox2.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                Display();
                MessageBox.Show("Username Saved");
            }
            catch (Exception)
            {
                MessageBox.Show("Error!");
            }
        }

        private void frmuser_Load(object sender, EventArgs e)
        {
            Display();
        }

        private void bunifuImageButton4_Click(object sender, EventArgs e)
        {
            try
            {
                int x = Convert.ToInt32(dgvUser.SelectedCells[0].Value);

                cmd.Parameters.Clear();
                cmd.Connection = con;
                cmd.CommandText = "Delete From Karbar where id=@N";
                cmd.Parameters.AddWithValue("@N", x);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                Display();
                MessageBox.Show("User Deleted!");
            }
            catch (Exception)
            {
                MessageBox.Show("Error!");
            }
        }

        private void dgvUser_SelectionChanged(object sender, EventArgs e)
        {

        }
    }
}

How can I overcome this error? 我该如何克服这个错误?

You are trying to access an element of an array that doesn't exist and is outside of the bounds. 您正在尝试访问不存在且在范围之外的数组元素。 Looking at your code, the Display method gets called on Load , so it appears the Columns index is the problem, ie you haven't set any columns up before trying to access them. 查看您的代码,在Load调用Display方法,因此看来Columns索引是问题所在,即您在尝试访问它们之前未设置任何列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:System.ArgumentOutOfRangeException:索引超出范围。 必须为非负数且小于集合的大小 - Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection System.ArgumentOutOfRangeException:'索引超出范围。 必须为非负数,并且小于集合的大小。” - System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.' System.ArgumentOutOfRangeException: '索引超出范围。 必须是非负的并且小于集合的大小。 参数名称:索引' - System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' System.ArgumentOutOfRangeException:索引超出范围。 必须是非负的并且小于集合的大小。 参数名称:索引 - System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ArgumentOutOfRangeException 索引超出范围。 必须是非负数且小于集合的大小。 参数名称:索引 - System.ArgumentOutOfRangeException Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 超出范围但它有效,索引超出范围。 必须是非负数且小于集合的大小 - Out of Range but it works, Index was out of range. Must be non-negative and less than the size of the collection 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:index - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 (参数‘索引’)" - Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM