【问题标题】:Using extjs4 with c# how do I access my data将 extjs4 与 c# 一起使用如何访问我的数据
【发布时间】:2011-08-25 14:07:53
【问题描述】:

我需要通过表单将用户数据添加到我的数据库中

所以我有一个带有这样的提交处理程序的表单:

xtype: 'fieldset',
        title: 'User information',
        defaults: {
            //width: 230,
            labelStyle: 'font-size:10px',
            labelWidth: 105
        },
        defaultType: 'textfield',
        layout : 'anchor',
        items: [{
            fieldLabel: 'Name',
            anchor: '98%',
            allowBlank: false,
            name: 'company'
        }, {
            fieldLabel: 'Last Name',
            anchor: '98%',
            name: 'price'
        }, {
            fieldLabel: 'Email ',
            anchor: '98%',
            allowBlank: false,
            vtype: 'email',
            name: 'pctChange'
        }

        ]
    },
             { border: false,
                 buttons: [{
                     text: 'Update user',
                     handler: function () {


                         {
                             if (this.up('form').getForm().isValid()) 
                            {                           
                               this.up('form').getForm().submit({url : 'save.cs' }); // this would submit the form to the configured url
                               //this.up('form').getForm().reset();
                               Ext.MessageBox.show({
                                 title: 'Success !',
                                 msg: 'Changes saved successfully<br />',
                                 icon: Ext.MessageBox.INFO
                                                    })                              
                             }

据我了解,提交函数调用了一个脚本(在我的例子中是 save.ashx),它应该更新我的数据库!

我有两个问题:

1-我如何访问我的数据(在表单中提交)我尝试在保存功能中添加参数,如下所示,但我不确定这一点

2-调用 save.ashx 时出现内部服务器错误

这是我的脚本:

 public class save
{
    SqlConnection dbConn = new SqlConnection("............");


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

    try
          {
              string str = "insert into UTILISATEUR  (firstname, lastname, 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();
                }

          }
       catch (Exception ex)
         {
             throw ex;
         }
    }
}

感谢您的宝贵时间

【问题讨论】:

    标签: c# database extjs4


    【解决方案1】:

    .cs 文件被 IIS 禁止 - 这些是 C# 源,不应由最终用户访问。 最终用户应该以这种方式或其他方式访问这些源的编译版本。例如。你可以创建 HTTP Handler(.ashx-file) 来处理你的请求。

    This 问题将帮助您了解有关将表单提交到 HTTP 处理程序的基础知识。

    【讨论】:

    • 谢谢..我确实创建了一个 .ashx 文件,但随后出现内部服务器错误
    • 我认为尝试逐步开发一切是有意义的。首先 - 工作 http 处理程序(默认工作室模板应该工作),第二 - 尝试提交表单并在服务器端(在调试器中)获取其值,第三 - 尝试将这些值传递给数据库。
    【解决方案2】:

    这永远不会起作用,因为 Li0liQ 说你不能执行 .cs 文件。

    C# 是一种编译语言,而不是 PHP 等“脚本语言”。

    我认为你应该阅读一本关于如何用 C# 开发 Web 应用程序的书。

    此外,您在 saveUser 中的代码将永远无法工作,因为

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

    你打开连接,然后检查状态,如果它打开,你关闭它,然后你执行你的命令。

    这没有意义。

      dbConn.Open();
    
      sqlCommand.ExecuteNonQuery();
    
      dbConn.Close();
    

    这样会更有意义。

    【讨论】:

    • 谢谢。我更改了它,并根据您的两个答案更新了我的问题。我正在努力学习 c#,因为我的截止日期很紧,谢谢你的耐心
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多