This is my procedure and below is code for updating and deleting a record.
But after clicking update or delete button its not deleting.
Please solve my doubt.
ALTER procedure [dbo].[StudentDetails]
(
@ID int=null,
@flag varchar(1),
@applicationno int=null,
@firstname varchar(25)=null,
@middlename varchar(25)=null,
@lastname varchar(25)=null,
@mothername varchar(25)=null,
@address varchar(500)=null,
@contactno varchar(15)=null,
@emailid varchar(25)=null,
@gender varchar(1)=null,
@tenthmarks decimal(6,2)=null,
@tewlthmarks decimal(6,2)=null,
@collegename varchar(35)=null,
@category varchar(10)=null,
@courses varchar(10)=null
)
As
Begin
if @flag='S'
begin
select * from Student_Master;
end
else
if @flag='I'
Begin
insert into Student_Master
values(@applicationno,@firstname,@middlename,@lastname,@mothername,@address,@contactno,@emailid,@gender,@tenthmarks,@tewlthmarks,@collegename,@category,@courses);
end
else
if @flag='U'
Begin
update Student_Master set applicatio_nno=@applicationno,fname=@firstname,mname=@middlename,lname=@lastname,mothername=@mothername,address=@address,contactno=@contactno,emailid=@emailid,Gender=@gender,tenthmarks=@tenthmarks,tewlthmarks=@tewlthmarks,collegename=@collegename,category=@category,courses=@courses
where ID=@ID;
end
else
if @flag='D'
begin
delete from Student_Master where ID=@ID;
end
end
This is my C# code:
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("");
if (!Page.IsPostBack)
BindData();
}
public void BindData()
{
if (con.State == ConnectionState.Closed)
con.Open();
try
{
cmd = new SqlCommand("StudentDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@flag", @SqlDbType.VarChar).Value = "S";
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Student_master");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
}
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
CallMyProcedure1("I");
}
public void CallMyProcedure1(string flag)
{
try
{
con.Open();
cmd = new SqlCommand("StudentDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@flag", SqlDbType.VarChar).Value = @flag;
cmd.Parameters.AddWithValue("@applicationno", SqlDbType.Int).Value = txtappsno.Text;
cmd.Parameters.AddWithValue("@firstname", SqlDbType.VarChar).Value = txtfname.Text;
cmd.Parameters.AddWithValue("@middlename", SqlDbType.VarChar).Value = txtmname.Text;
cmd.Parameters.AddWithValue("@lastname", SqlDbType.VarChar).Value = txtlastname.Text;
cmd.Parameters.AddWithValue("@mothername", SqlDbType.VarChar).Value = txtmothername.Text;
cmd.Parameters.AddWithValue("@address", SqlDbType.VarChar).Value = txtaddress.Text;
cmd.Parameters.AddWithValue("@contactno", SqlDbType.VarChar).Value = txtcontactno.Text;
cmd.Parameters.AddWithValue("@emailid", SqlDbType.VarChar).Value = txtemailid.Text;
string gender = "";
if (rdbmale.Checked == true)
gender = "M";
else
gender = "F";
cmd.Parameters.AddWithValue("@gender", SqlDbType.VarChar).Value = gender;
cmd.Parameters.AddWithValue("@tenthmarks", SqlDbType.Decimal).Value = txttenth.Text;
cmd.Parameters.AddWithValue("@tewlthmarks", SqlDbType.Decimal).Value = txttwelth.Text;
cmd.Parameters.AddWithValue("@collegename", SqlDbType.VarChar).Value = txtclgname.Text;
string cast = "";
if (rdbopen.Checked == true)
cast = "Open";
else if (rdbsc.Checked == true)
cast = "SC";
else if (rdbobc.Checked == true)
cast = "OBC";
else if (rdbnt.Checked == true)
cast = "NT";
cmd.Parameters.AddWithValue("@category", SqlDbType.VarChar).Value = cast;
cmd.Parameters.AddWithValue("@courses", SqlDbType.VarChar).Value = drpcourses.Text;
cmd.ExecuteNonQuery();
BindData();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
txtid.Text = GridView1.SelectedRow.Cells[1].Text;
txtappsno.Text = GridView1.SelectedRow.Cells[2].Text;
txtfname.Text = GridView1.SelectedRow.Cells[3].Text;
txtmname.Text = GridView1.SelectedRow.Cells[4].Text;
txtlastname.Text = GridView1.SelectedRow.Cells[5].Text;
txtmothername.Text = GridView1.SelectedRow.Cells[6].Text;
txtaddress.Text = GridView1.SelectedRow.Cells[7].Text;
txtcontactno.Text = GridView1.SelectedRow.Cells[8].Text;
txtemailid.Text = GridView1.SelectedRow.Cells[9].Text;
if (GridView1.SelectedRow.Cells[10].Text=="M")
rdbmale.Checked = true;
else
rdbmale.Checked=false;
txttenth.Text = GridView1.SelectedRow.Cells[11].Text;
txttwelth.Text = GridView1.SelectedRow.Cells[12].Text;
txtclgname.Text = GridView1.SelectedRow.Cells[13].Text;
if (GridView1.SelectedRow.Cells[14].Text == "OPEN")
rdbopen.Checked = true;
else if (GridView1.SelectedRow.Cells[14].Text == "OBC")
rdbobc.Checked = true;
else if (GridView1.SelectedRow.Cells[14].Text == "SC")
rdbsc.Checked = true;
else if (GridView1.SelectedRow.Cells[14].Text == "NT")
rdbnt.Checked = true;
drpcourses.Text = GridView1.SelectedRow.Cells[15].Text;
}
protected void btnupdate_Click(object sender, EventArgs e)
{
CallMyProcedure1("U");
BindData();
}
protected void btndelete_Click(object sender, EventArgs e)
{
CallMyProcedure1("D");
BindData();
}