通过过程更新数据 [英] Updating a data through Procedure

查看:61
本文介绍了通过过程更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的程序,下面是更新和删除记录的代码。

但点击更新或删除按钮后不删除。



请解答我的疑问。

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



这是我的C#代码:


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();
  }

推荐答案

您没有发送 @ID 参数来自您的代码更新删除将起作用。



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

Thanks


这篇关于通过过程更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆