I am trying an editable grid but it showing some error which I cant trace..please help me .I am getting these errors when...
Update
Additional information: Procedure or function sp_neetha has too many arguments specified.
Delete
Additional information: The connection was not closed. The connection's current state is open.
my code is here...
MY STORED PROCEDURE FOR TABLE NAME NEETHA( PLS READ MY DOUBTS WHICH IS COMMENTED)
create procedure sp_neetha (@id int ,@school nchar(10),@office nchar(10),@flag nchar(10))
as
begin
if(@flag='insert')
begin
insert into neetha (id,school ,office) values(@id,@school,@office)
end
if(@flag='delete')
begin
delete from neetha where id=@id
end
if(@flag='select')
begin
select * from neetha
end
if(@flag='update')
begin
update neetha set school=@school,office=@office where id=@id
end
end
C# CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace joins
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
grid();
}
}
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\MyPractice\joins\joins\App_Data\Database1.mdf;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
public void grid()
{
con.Open();
cmd.Connection = con;
cmd.CommandText = "sp_neetha";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id",DbType.Int32);
cmd.Parameters.AddWithValue("@school", DbType.String);
cmd.Parameters.AddWithValue("@office", DbType.String);
cmd.Parameters.AddWithValue("@flag", "select");
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
grid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());
TextBox txtid = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txteditid");
TextBox txtschool = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txteditscool");
TextBox txtoffs = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txteditoffs");
con.Open();
cmd.Connection = con;
cmd.CommandText = "sp_neetha";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", txtid.Text);
cmd.Parameters.AddWithValue("@school", txtschool.Text);
cmd.Parameters.AddWithValue("@office", txtoffs.Text);
cmd.Parameters.AddWithValue("@flag","update");
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
grid();
}
protected void Button6_Click(object sender, EventArgs e)
{
grid();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id1= Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());
con.Open();
cmd.Connection = con;
cmd.CommandText = "sp_neetha";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", DbType.Int32);
cmd.Parameters.AddWithValue("@school", DbType.String);
cmd.Parameters.AddWithValue("@office", DbType.String);
cmd.Parameters.AddWithValue("@flag", "delete");
grid();
int result = cmd.ExecuteNonQuery();
if (result==1)
{
grid();
}
con.Close();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.Equals("add"))
{
TextBox txtid1 = (TextBox)GridView1.FooterRow.FindControl("txteditfooter");
TextBox txtschool1 = (TextBox)GridView1.FooterRow.FindControl("txteditscool");
TextBox txtoffs1 = (TextBox)GridView1.FooterRow.FindControl("txteditfooterofs");
con.Open();
cmd.Connection = con;
cmd.CommandText = "sp_neetha";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", txtid1.Text);
cmd.Parameters.AddWithValue("@school", txtschool1.Text);
cmd.Parameters.AddWithValue("@office", txtoffs1.Text);
cmd.Parameters.AddWithValue("@flag", "insert");
int result = cmd.ExecuteNonQuery();
if(result==1)
{
GridView1.EditIndex = -1;
grid();
}
con.Close();
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
grid();
}
}
}