Click here to Skip to main content
12,445,004 members (53,210 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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;
             
            }
        
        }
    }
Posted 1-Jan-13 23:37pm
Updated 21-Mar-16 23:45pm
v3
Comments
Mrugesh08 2-Jan-13 6:00am
   
plz help me sir......
Mrugesh08 2-Jan-13 5:52am
   
when im selecting a record from gridview..show me the error Index 0 is either negative or above rows count...
Mrugesh08 2-Jan-13 5:54am
   
plz help me ..my code is above..
Turbo_23 2-Jan-13 6:05am
   
hi,
post your complete code & error message here
jibesh 2-Jan-13 6:06am
   
on executing which line you are getting this error?
Mrugesh08 2-Jan-13 6:38am
   
job is is numeric ...and when im selecting record...then give me error on the line
txtid.Text = dv[0]["visit_id"].ToString();...says index o is either negative or above rows count...
jibesh 2-Jan-13 6:46am
   
this is because dv array has zero rows when you Apply RowFilter. check my updated solution.
Simon_Whale 2-Jan-13 6:40am
   
what method are you running this from? how is this code run? a button?

you need to provide more information to gain help as we dont know how your project is setup
Mrugesh08 2-Jan-13 6:42am
   
i have post all my code....
@AmitGajjar 2-Jan-13 6:45am
   
you can check if your dv contains some data or not.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The reason is when you set row filter on this line
dv.RowFilter = "job_id='" + s1 + "'";
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();
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,

Your error is at
dv.RowFilter = "job_id='" + 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
  Permalink  
Comments
Mrugesh08 2-Jan-13 7:01am
   
s1 = e.Item.Cells[4].Text; im assigning this s1 sir...means job is is stored in s1 and comparing that value...whether the job_id is equal to s1(job_id) or not..and if equal select all the record of that id from database...
Mrugesh08 2-Jan-13 7:18am
   
no sir...dv.length is not exist ..show me error....
jibesh 2-Jan-13 23:55pm
   
did you check dv.Count. since datatype DV is not at that time i said either count or Length should be available.
Mrugesh08 2-Jan-13 7:22am
   
in row filter im getting job_id value..my row filter in not null...in row filter job_id value is show..but very next line show me error..
Mrugesh08 2-Jan-13 7:53am
   
plz sir help me...
Mrugesh08 3-Jan-13 0:07am
   
ya sir ..try....working....thank you..
Mrugesh08 3-Jan-13 0:10am
   
not working sir..same error...
Pallav Jha 22-Mar-16 5:53am
   
//GET STD Deviation VALUE FROM DATASET

DataView dv3 = dbSet.Tables[0].DefaultView;
dv.RowFilter = "TPMVALUE = STDEV(TPMVALUE)";
decimal stddev = Convert.ToDecimal(dv2[0]["TPMVALUE"]);

Index 0 is either negative or above rows count Getting this error
ppolymorphe 22-Mar-16 6:03am
   
This question is 2013;
What do you expect with this comment ?
if it is a question, ask a new question, (see on top Quick Answers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160811.3 | Last Updated 22 Mar 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100