数据列表中的更新问题 [英] problem with update in datalist

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

问题描述

你好,

我目前正在使用数据列表通过文本框控件更新数据库.
问题是,当我按编辑"并在文本框中输入值时,以及尝试更新数据库时,数据库不会使用输入的值进行更新.

有人可以让我知道使用文本框中的值更新数据库的确切过程吗?

Hello,

I am currently using a datalist to update the database with a textbox control.
The problem is that when I am pressing "Edit" and entering values in the textbox and when trying to update the database, the database is not getting updated with the entered value.

Could someone please let me know the exact procedure to update the database with the value from the textbox?

<asp:DataList ID="DataList1" runat="server"  DataKeyField="memberID"  

       oneditcommand="DataList1_EditCommand" 

        onupdatecommand="DataList1_UpdateCommand">







private void   Load(){
         dal db = new dal();
        DataTable dt = new DataTable();
        dt = db.run_dr("SELECT [memberID], [Name], [Family], [Tell], [Email]);
        DataList1.DataSource = dt;
        DataList1.DataBind();


 protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
       TextBox txtName = (TextBox)e.Item.FindControl("TextName");
       TextBox txtFamily = (TextBox)e.Item.FindControl("TextFamily");
       TextBox txtTell = (TextBox)e.Item.FindControl("TextTell");

if (Page.IsValid)
        {
            db.run_dr("update members set Name=N'" + txtName.Text +
                                          "',Family=N'" + txtFamily.Text +
                                          "',Tell='" + txtTell.Text.Trim() +
                                          "' where  memberID="+                        DataList1.DataKeys[e.Item.ItemIndex].ToString());

            DataList1.DataBind();
            DataList1.EditItemIndex = -1;
        }
    }



问题是文本框中更改的值未得到反映.



Problem is the altered value in the textbox is not getting reflected.

protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
   {

       DataList1.EditItemIndex = e.Item.ItemIndex;
       Load();
   }

推荐答案

您是否尝试过输入实际ID以测试DataList1.DataKeys [e.Item.ItemIndex] .ToString()是否确实为您提供了帮助您的where子句上的ID?并尝试将此DataList1.EditItemIndex = -1;在DataList1.DataBind()之前;
Have you tried to put an actual ID to test if DataList1.DataKeys[e.Item.ItemIndex].ToString() does really gives you the ID on your where clause? and try to put this DataList1.EditItemIndex = -1; before DataList1.DataBind();


我不确定为什么会这样,但是我发现了代码中的一些错误.可能是因为该原因导致未插入值.尝试用以下命令替换您的命令:
I am not sure why it is happening, but I found some of the bugs in your code. May be the values are not getting inserted because of that. Try replacing your command with this:
db.run_dr("update members set Name='" + txtName.Text +
                                          "',Family='" + txtFamily.Text +
                                          "',Tell='" + txtTell.Text.Trim() +
                                          "' where  memberID='"+                        DataList1.DataKeys[e.Item.ItemIndex].ToString()+"'");



希望对您有所帮助.
--Amit



Hope it helps.
--Amit


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

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