Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 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:
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();
  }
Posted 1-Jan-13 0:47am
Edited 1-Jan-13 1:01am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

Thanks
  Permalink  
Comments
Turbo_23 at 1-Jan-13 8:12am
   
Great catch....
AshishChaudha at 3-Jan-13 23:54pm
   
Thanks....no votes! :)
Turbo_23 at 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)

  Print Answers RSS
0 OriginalGriff 215
1 Sebastiaan Meijerink 141
2 Sergey Alexandrovich Kryukov 124
3 Rana Waqas 85
4 Maciej Los 84
0 Sergey Alexandrovich Kryukov 6,558
1 OriginalGriff 6,288
2 Peter Leow 2,534
3 Abhinav S 2,358
4 Maciej Los 2,352


Advertise | Privacy | Mobile
Web02 | 2.8.150414.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100