简体   繁体   中英

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. Must be non-negative and less than the size of the collection

Scenario: A desktop application that reads data from the SQL Server database. 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.

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