繁体   English   中英

extjs / c#-如何将数据从表单保存到数据库?

[英]extjs/c# - how to save data from a form to a database?

我试图通过表单将新用户添加到数据库中,但是我的问题是如何将表单中的数据保存到数据库中? 即时通讯使用C#和extjs4.0.1

我知道我需要在按钮上使用Submit并为其提供一个URL,但我单击该按钮时什么也没有发生! 没有错误! 我得到成功信息框! 但是当我检查数据库时,没有插入任何内容!像我的updateuser函数从未被调用过!!!

这是我的表单脚本:

buttons: [{
                     text: 'Save user',
                     handler: function () {
                             if (this.up('form').getForm().isValid())
                                 this.up('form').getForm().submit
                                 ({// this would submit the form to the configured url
                                     url: 'update.ashx',
                                     success: function (form, action) {
                                         Ext.MessageBox.show({
                                             title: 'Success !',
                                             msg: 'Changes saved successfully<br />',
                                             icon: Ext.MessageBox.INFO
                                         })
                                     },                                        
                                 });
                                 this.up('form').getForm().reset();

                     }

这是我的update.ashx脚本:

public class update : IHttpHandler
{
    SqlConnection dbConn = new SqlConnection("....");

    public void ProcessRequest(HttpContext context)
    {        }

    public bool IsReusable
    {
        get           {          return false;            }
    }

    public void updateUser(string firstname, string lastname, string email)
    {


        string str = "insert into User  (Name , FName , Email ) values ( '" + firstname + "' , '" + lastname + "' , '" + email + "' )";

        SqlCommand sqlCommand = new SqlCommand(str);
        sqlCommand.Connection = dbConn;
        sqlCommand.CommandType = CommandType.Text;
        SqlDataAdapter sda = new SqlDataAdapter(sqlCommand);
        dbConn.Open();

        if (sqlCommand.Connection.State == ConnectionState.Open)
        {
            sqlCommand.ExecuteNonQuery();
            dbConn.Close();
        }
    }

}

你有什么主意吗? 至少让我知道即时消息是否正确...谢谢

首先是“成功:函数(形式,动作)”,此函数调用并不意味着更新用户是成功,而是提交成功..为了成功,您需要编码一个成功为true的json,然后action.result.success将确定成功是否正确...检查api .....

其次,我不是ac#家伙...但是您似乎根本没有处理请求

编辑:

为了避免重复的问题,请向处理程序发送一个额外的参数...仅在收到请求中的更新操作时才执行update方法

url: 'update.ashx',
params: {action: 'update'},
success: function (form, action) {

表单提交将表单值发送到上下文对象包含的HttpRequest对象内的处理程序。要检索表单值,您可以检查http://msdn.microsoft.com/zh-cn/library/system.web.ihttphandler .processrequest.aspx示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM