Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting this error when selecting a record from grid view.

My code is:
C#
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
Updated 21-Mar-16 23:45pm
v3
Comments
[no name] 2-Jan-13 5:48am    
Please share your error here too...
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..
[no name] 2-Jan-13 6:04am    
Tell me what is the datatype of "job_id" and what is dv in your code?

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)

C#
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();
}
 
Share this answer
 
v2
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
 
Share this answer
 
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...
[no name] 2-Jan-13 7:08am    
I understand that... As your job_id is of numeric type and you are comparing that using a string that's why the row count is coming 0 and you are getting an error. Does your code work fine with the solution given by jibesh? Or you are not getting the error only? Plz reply...
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..

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900