Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
SQL
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 applicatio_nno=@applicationno
		where ID=@ID;
		end
	else	
	if @flag='D'
	begin
		--delete from Student_Master where applicatio_nno=@applicationno
		delete from Student_Master where ID=@ID;
	end
end

This is my C# code:
C#
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();
    }

}


C#
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;

   }


C#
protected void btnupdate_Click(object sender, EventArgs e)
  {
      CallMyProcedure1("U");
      BindData();
  }
  protected void btndelete_Click(object sender, EventArgs e)
  {

      CallMyProcedure1("D");
      BindData();
  }
Posted
Updated 1-Jan-13 0:01am
v3

1 solution

You are not sending @ID parameter from your code through which update or Delete will work.

Thanks
 
Share this answer
 
Comments
[no name] 1-Jan-13 8:12am    
Great catch....
AshishChaudha 3-Jan-13 23:54pm    
Thanks....no votes! :)
[no name] 4-Jan-13 0:05am    
If you don't want to vote on such solutions then don't post your code for solutions.

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