narasiman rao

narasiman rao

  • NA
  • 519
  • 771.5k

when i insert a value using grid view i get the following error occurs

Sep 9 2012 9:13 AM
when i insert a value using grid view i get the following error  occurs as follows
  specified argument was out of range of valid values parameter name index


My Code as follows;

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Server=(local);initial catalog=Pipe;Trusted_Connection=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter("Select pipno,RPTUNIT,YYYYMM From PIPEntry", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DataRow dr = dt.NewRow();
        dt.Rows.InsertAt(dr, 0);
        GridView1.EditIndex = 0;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        ((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text == "Insert")
        {

            SqlCommand cmd = new SqlCommand();
            //cmd.CommandText = "Insert into PIPEntry(pipno,RPTUNIT,YYYYMM)values@pipno,@RPTUNIT,@YYYYMM)";
            cmd.CommandText = "INSERT INTO PIPEntry(pipno,RPTUNIT,YYYYMM) VALUES(@pipno,@RPTUNIT,@YYYYMM)";
            //cmd.Parameters.Add("@pipno", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[1].Controls[0]).Text;
            cmd.Parameters.Add("@pipno", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[1].Controls[0]).Text;
            cmd.Parameters.Add("@RPTUNIT", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[2].Controls[0]).Text;
            cmd.Parameters.Add("@YYYYMM", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text;
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        else
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "UPDATE PIPEntry SET RPTUNIT=@RPTUNIT, YYYYMM=@YYYYMM WHERE pipno=@pipno";
            cmd.Parameters.Add("@RPTUNIT", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            cmd.Parameters.Add("@pipno", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            cmd.Parameters.Add("@YYYYMM", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        GridView1.EditIndex = -1;
      



    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }


    private void BindData()
    {

        SqlDataAdapter da = new SqlDataAdapter("SELECT pipno,RPTUNIT,YYYYMM FROM PIPEntry", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "DELETE FROM PIPEntry WHERE pipno=@pipno";
        cmd.Parameters.Add("@pipno", SqlDbType.VarChar).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        BindData();
    }
}
   

In the above wat is the error please help me. i inserted the four values but when i insert a 5 th values  i getting the above error.


And send the correct code.please help me.

Regards,RAO.,,












Answers (1)