单击按钮更新数据库中的数据

时间:2012-11-05 08:52:54

标签: asp.net sql-server

我有一个kendo下拉列表和选定的id值我想在按钮点击事件中更新数据库中特定行的数据。但是我的dat没有更新,旧值一次又一次地显示。请指教我怎么做它。 我的代码是

<script src="Js/jquery1.7.1.min.js" type="text/javascript"></script>
<script src="CSS/kendo.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
  $(document).ready(function(){
    $("#multi").kendoDropDownList();
    $("#editor").kendoEditor();
 });
</script>
<form id="form1" runat="server">
<div>
   Select Template: <select id="multi" runat="server"></select>
</div>
<br />
<div>
  Template Name: <input type="text" id="txtName" name="Name" runat="server" />
</div>
<div>
Description:<textarea name="comments" id="txtComment" cols="25" rows="5"   
runat="server"></textarea>
<br />
</div>
<div>
Active: <input type="checkbox" id="chkActive" name="vehicle" runat="server" />
</div>
<div id="example" class="k-content">
<textarea id="editor" rows="10" cols="30" style="width:740px;height:440px"   
runat="server"></textarea>
</div>
<div>

<button type="button" id="btnUpdate" runat="server"   onserverclick="    
btnUpdate_onclick" >Save</button>

</div>
</form>
</body>

我的.cs代码是

 protected void Page_Load(object sender, EventArgs e)
{


    Bind();
}

private void Bind()
{
    SqlConnection con = new SqlConnection("Data Source=ST0022;Initial 
Catalog=QuickMove_Globe;User ID=sa;Password=good");
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    da.SelectCommand = new SqlCommand("Select * from Templates", con);
    con.Open();
    da.SelectCommand.ExecuteNonQuery();
    da.Fill(ds);
    con.Close();
    multi.DataTextField = "ID";
    multi.DataValueField = "Name";
    multi.DataValueField = "Description";

    multi.DataSource = ds.Tables[0];
    multi.DataBind();
}

protected void btnUpdate_onclick(object sender, EventArgs e)
{



    SqlConnection con = new SqlConnection(@"Data Source=ST0022;Initial 
Catalog=QuickMove_Globe;Persist Security Info=True;User ID=sa;Password=good");
    SqlCommand cmd= new SqlCommand("Update_Temp", con);

    cmd.CommandType = CommandType.StoredProcedure;
    //passing parameters
    cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(multi.SelectedIndex));
    cmd.Parameters.AddWithValue("@Name",Convert.ToString(txtName.Value));
    cmd.Parameters.AddWithValue("@Description", Convert.ToString(txtComment.Value));
    cmd.Parameters.AddWithValue("@Content", Convert.ToString(editor.Value));
    cmd.Parameters.AddWithValue("@Active", (chkActive.Value).ToString());

    SqlDataAdapter da = new SqlDataAdapter();

    da.UpdateCommand=cmd;
    con.Open();






    cmd.ExecuteNonQuery();
    con.Close();

}

2 个答案:

答案 0 :(得分:0)

检查下拉列表中的正确值是否为代码隐藏方法。您可以更改按钮命令,如下所示:

<asp:Button ID="btnUpdate" runat="server" 
            onclick="btnUpdate_Click" Text="Save" />

答案 1 :(得分:0)

您可能想考虑添加一些错误处理,但试试这个。你不需要存储过程中的RETURN。你需要做的其他事情就是理清你的绑定。您将错误的值绑定到下拉列表。无论您想要显示什么,值都必须是ID和Text。然后在更新中Convert.ToInt32(multi.SelectedIndex)应该是SelectedValue而不是SelectedIndex。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       Bind();
    }

}



        protected void btnUpdate_onclick(object sender, EventArgs e)
    {



        SqlConnection con = new SqlConnection(@"Data Source=ST0022;Initial 
 Catalog=QuickMove_Globe;Persist Security Info=True;User ID=sa;Password=good");
        SqlCommand cmd = new SqlCommand("Update_Temp", con);

        cmd.CommandType = CommandType.StoredProcedure;
        //passing parameters
        cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(multi.SelectedIndex));
        cmd.Parameters.AddWithValue("@Name", Convert.ToString(txtName.Value));
        cmd.Parameters.AddWithValue("@Description", Convert.ToString(txtComment.Value));
        cmd.Parameters.AddWithValue("@Content", Convert.ToString(editor.Value));
        cmd.Parameters.AddWithValue("@Active", (chkActive.Value).ToString());
        con.Open();

        cmd.ExecuteNonQuery();
        con.Close();

        Bind();
    }