索引0为负数或高于行数。 [英] Index 0 is either negative or above rows count.

查看:191
本文介绍了索引0为负数或高于行数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从网格视图中选择记录时出现此错误。



我的代码是:

I am getting this error when selecting a record from grid view.

My code is:

public partial class jobview : System.Web.UI.Page
{
    
    
    jobBAL objbal = new jobBAL();
    jobDAL objdal = new jobDAL();
    static DataView dv;
    protected void Page_Load(object sender, EventArgs e)
    {
        txtdate.Text = System.DateTime.Now.ToShortDateString();
        btnupdate.Visible = false;
        string username;
        username = Session["cuname"].ToString();

        Label4.Text = username;             
        dv = new DataView();
    }
    void showgrid()
    {

        RadGrid1.Rebind();
       
    }

    protected void RadButton1_Click(object sender, EventArgs e)
    {
        objdal.visit_id = Convert.ToInt32(txtid.Text);
        objdal.date = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
        objdal.radiologist = drpradio.Text;
        objdal.study_type = drpstudy.Text;
        objdal.account = drpaccount.Text;

        objdal.time_duration = Convert.ToDateTime(txtaudio.Text);
        objdal.uname = Label4.Text;

        objdal.remark = txtremarks.Text;

        if (radiobtn.Items[0].Selected)
        {
            objdal.upload = radiobtn.Items[0].Text;

        }

        else if (radiobtn.Items[1].Selected)
        {
            objdal.upload = radiobtn.Items[1].Text;

        }
        else if (radiobtn.Items[2].Selected)
        {
            objdal.upload = radiobtn.Items[2].Text;
        }

        objbal.addrec(objdal);
        Label3.Text = "Congratulations!!Data Inserted Sucessfully";
        showgrid();
       


        txtdate.Text = "";
        txtid.Text = "";
        txtremarks.Text = " ";
        txtaudio.Text = "";
        drpradio.ClearSelection();
        drpstudy.ClearSelection();
        drpaccount.ClearSelection();
       

    }
    protected void RadButton1_Click1(object sender, EventArgs e)
    {
        objdal.job_id = Convert.ToInt16(txtpid.Text);
        objdal.visit_id = Convert.ToInt32(txtid.Text);
        objdal.date = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
        objdal.radiologist = drpradio.SelectedItem.Text;
        objdal.study_type = drpstudy.SelectedItem.Text;
        objdal.account = drpaccount.SelectedItem.Text;
        objdal.time_duration = Convert.ToDateTime(txtaudio.Text);

        objdal.remark = txtremarks.Text;
        objdal.uname = Label4.Text;

        if (radiobtn.Items[0].Selected)
        {
            objdal.upload = radiobtn.Items[0].Text;

        }

        else if (radiobtn.Items[1].Selected)
        {
            objdal.upload = radiobtn.Items[1].Text;

        }
        else if (radiobtn.Items[2].Selected)
        {
            objdal.upload = radiobtn.Items[2].Text;
        }

        objbal.updaterec(objdal);
        Label3.Text = "Congratulations!!Data Updated Sucessfully";
        showgrid();


        txtdate.Text = "";
        txtid.Text = "";
        txtremarks.Text = " ";
        txtaudio.Text = "";
        drpradio.ClearSelection();
        drpstudy.ClearSelection();
        drpaccount.ClearSelection();

        btnsave.Visible = true;

    }
    protected void btnreset_Click(object sender, EventArgs e)
    {
        txtdate.Text = "";
        txtid.Text = "";
        txtremarks.Text = " ";
        txtaudio.Text = "";
        drpradio.ClearSelection();
        drpstudy.ClearSelection();
        drpaccount.ClearSelection();

        btnsave.Visible = true;
        
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

        ViewState["btnid"] = "del";
    }

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {

        ViewState["btnid"] = "sel";
        RadGrid1.Visible = true;
    }
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        string s, s1;

        if (ViewState.Count > 0)
        {
            s = ViewState["btnid"].ToString();
            s1 = e.Item.Cells[4].Text;
            ViewState["job_id"] = s1;

            if (s == "del")
            {

                objdal.job_id = Convert.ToInt16(ViewState["job_id"].ToString());
                objbal.deleterec(objdal);
                Label3.Text = "Record Deleted Sucessfully...";
                showgrid();
            }

            else if (s == "sel")
            {
                txtpid.Text = s1;
                dv.RowFilter="job_id='" + s1 + "'";
                txtid.Text = dv[0]["visit_id"].ToString();
                txtdate.Text = dv[0]["date"].ToString();
                drpradio.SelectedValue = dv[0]["radiologist"].ToString();
                drpstudy.SelectedValue = dv[0]["study_type"].ToString();
                drpaccount.SelectedValue = dv[0]["account"].ToString();
                radiobtn.SelectedValue = dv[0]["upload"].ToString();
                txtaudio.Text = dv[0]["time_duration"].ToString();
                txtremarks.Text = dv[0]["remark"].ToString();
                Label4.Text = dv[0]["uname"].ToString();
                btnupdate.Visible = true;
                btnsave.Visible = false;
             
            }
        
        }
    }

推荐答案

原因是当你在这一行设置行过滤器时

The reason is when you set row filter on this line
dv.RowFilter = "job_id='" + s1 + "'";

dv不包含任何匹配的行,因此它是空的。即dv有零行。

在此过滤器之后,您尝试访问零索引值。检查dv.Length或Count(我不确定dv是什么数据类型)



dv doesnt contains any matching rows hence its empty. i.e dv has zero rows.
following this filter you are trying to access a zero index value. check the dv.Length or Count ( i am not sure about what data type dv is)

if( dv.Count > 0)
{
  txtid.Text = dv[0]["visit_id"].ToString();
  txtdate.Text = dv[0]["date"].ToString();
  drpradio.SelectedValue = dv[0]["radiologist"].ToString();
  drpstudy.SelectedValue = dv[0]["study_type"].ToString();
  drpaccount.SelectedValue = dv[0]["account"].ToString();
  radiobtn.SelectedValue = dv[0]["upload"].ToString();
  txtaudio.Text = dv[0]["time_duration"].ToString();
  txtremarks.Text = dv[0]["remark"].ToString();
  Label4.Text = dv[0]["uname"].ToString();
}






您的错误在
Hi,

Your error is at
dv.RowFilter = "job_id='" + s1 + "'";

point。



job_id的数据类型为数字为你提到的。您将通过s1将字符串值分配给过滤器。将s1的代码更改为数字,您的问题将得到解决。



谢谢

point.

The datatype of the job_id is numeric as mentioned by u. And you are assigning the string value to the filter by s1. Change your code for s1 as numeric and your problem will be solved.

Thanks


这篇关于索引0为负数或高于行数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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