为何“指数超出范围。必须是非负数且小于集合的大小“选择网格视图选择按钮时,以下程序会出现错误 [英] Why "Index was out of range. Must be non-negative and less than the size of the collection"error comes on the following program on selecting grid view select butten

查看:57
本文介绍了为何“指数超出范围。必须是非负数且小于集合的大小“选择网格视图选择按钮时,以下程序会出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Doctor_details : System.Web.UI.Page
{
    Class1 ob = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            grdv();
            binddept();
            clear();
        }
    }
    public void grdv()
    {
        ob.grdv("select * from doctor_details order by drcode", GridView1);
    }
    public void binddept()
    {
        ob.fetch("select * from doctor_dept order by deptid");
        drpdeptnm.Items.Clear();
        drpdeptnm.DataSource = ob.ds.Tables[0];
        drpdeptnm.DataTextField = "dept";
        drpdeptnm.DataBind();
        drpdeptnm.Items.Insert(0, "select");
    }
    public void clear()
    {
        txtdrcode.Text = "";
        txtdrnm.Text ="";
        txtadd.Text ="";
        txtemail.Text ="";
        txtmob.Text ="";
        txtdrnm.Focus();
        drpdeptnm.SelectedIndex = 0;
        txtpwd.Text = "";
        //txtdrcode.ReadOnly = true;
     }
    static string gender = "";
    protected void  btnsubmit_Click(object sender, EventArgs e)
{
        if (txtdrcode.Text.Trim() == "")
        {
            lblmsg.Text = "Doctor code  should not be blank";
            txtdrcode.Focus();
            return;
        }
        if (txtdrnm.Text.Trim() == "")
        {
            lblmsg.Text = "Doctor name  should not be blank";
            txtdrnm.Focus();
            return;
        }
        if (drpdeptnm.SelectedIndex == 0)
        {
            lblmsg.Text = "Department must be selected";
            drpdeptnm.Focus();
            return;
        }
        if (txtadd.Text.Trim() == "")
        {
            lblmsg.Text = "Address  should not be blank";
            txtadd.Focus();
            return;
        }
        if (txtemail.Text.Trim() == "")
        {
            lblmsg.Text = "Email  should not be blank";
            txtemail.Focus();
            return;
        }
        
        
        if (txtmob.Text.Trim() == "")
        {
            lblmsg.Text = "Mobile number should not be blank";
            txtmob.Focus();
            return;
        }
        if (rdbgend.Checked == true)
        {
            lblmsg.Text = "Select the gender";
            rdbgend.Focus();
        }
        else
        {
            lblmsg.Text = "Select the gender";
            rdbgend.Focus();
        }
        if (txtpwd.Text.Trim() == "")
        {
            lblmsg.Text = "Password should not be blank";
            txtpwd.Focus();
            return;
        }

        if (rdbgend.Checked == true)
        {
            gender = rdbgend.Text;
        }
        else
        {
            gender = RadioButton2.Text;
        }
        ob.fetch("select drcode from doctor_details where drcode='" + txtdrcode.Text + "'");
        if (ob.ds.Tables[0].Rows.Count > 0)
        {
            lblmsg.Text = "already exits";
            txtdrcode.Text = "";
            txtdrcode.Focus();

        }
        else
        {
            if (ob.dml_statment("insert into doctor_details values('" + txtdrcode.Text.Trim() + "','" + txtdrnm.Text.Trim() + "', '" + drpdeptnm.SelectedItem.Text + "','" + txtadd.Text.Trim() + "','" + txtemail.Text.Trim() + "','" + txtmob.Text.Trim() + "','" + gender + "','" + txtpwd.Text.Trim() + "')") > 0)
            {
                lblmsg.Text = "Data Submitted!";
                clear();
                grdv();
            }
            else
            {
                lblmsg.Text = "Data Can't Submit";
            }
        }
  }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtdrcode.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[0].Text;
        txtdrnm.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
        drpdeptnm.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
        txtadd.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text;
        txtemail.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text;
        txtmob.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text;
        txtpwd.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[7].Text;
        //txtdrcode.ReadOnly = true;
        if (GridView1.Rows[GridView1.SelectedIndex].Cells[6].Text=="Male")
        {
            rdbgend.Checked = true;
            RadioButton2.Checked = false;
        }
        else
        {
            RadioButton2.Checked = true;
            rdbgend.Checked = false;
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (ob.dml_statment("delete from doctor_details where drcode='" + GridView1.Rows[e.RowIndex].Cells[0].Text + "'") > 0)
        {
            lblmsg.Text = "Data deleted";
            grdv();
            clear();
        }
    }
    protected void btnupd_Click(object sender, EventArgs e)
    {
        if (ob.dml_statment("update doctor_details set drname='" + txtdrnm.Text.Trim() + "', address='" + txtadd.Text.Trim() + "', emailId='" + txtemail.Text.Trim() + "', mob='" + txtmob.Text.Trim() + "' where drcode='" + txtdrcode.Text.Trim() + "'") > 0)
        {
            lblmsg.Text = "Data updated";
            grdv();
            clear();
        }
    }
    protected void btncancel_Click(object sender, EventArgs e)
    {
        txtdrcode.Text ="";
        txtdrnm.Text  = "";
        drpdeptnm.SelectedIndex = 0;
        txtadd.Text  = "";
        txtemail.Text = "";
        txtmob.Text  = "";
        txtpwd.Text  = "";
        txtdrnm.Focus();
        txtdrcode.ReadOnly = true;

    }
    protected void txtemail_TextChanged(object sender, EventArgs e)
    {

    }
}

推荐答案

尝试分析异常。

Try to analyze the Exception.
Index was out of range. Must be non-negative and less than the size of the collection



所以,它表示你引用的是一些索引,它在 GridView中不存在



正如您所提到的,当您单击选择按钮时会发生这种情况。因此,调试以下事件。


So, it says you are referring some index, which is not present inside GridView.

As you have mentioned it happens when you click the Select Button. So, debug the following Event.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)



当你使用<$ c逐行移动时$ c> F10 ,您可以看到您正在阅读的值。



查看它抛出异常的行。检查您在该行代码上使用的索引。纠正。


As you move line by line using F10, you can see the values which you are reading.

See on which line it is throwing the Exception. Check which index you are using on that line of code. Correct that.


这篇关于为何“指数超出范围。必须是非负数且小于集合的大小“选择网格视图选择按钮时,以下程序会出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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