我正在尝试从aql数据库中的asp.net psge注册来注册用户,这是我在下面编写代码时遇到的错误msg. [英] I was tring to register to register users from asp.net psge in aql database and it is gennerating error msg i wrote bellow the code.

查看:73
本文介绍了我正在尝试从aql数据库中的asp.net psge注册来注册用户,这是我在下面编写代码时遇到的错误msg.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
 
public partial class Test_trail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 private void ExecuteInsert(string name, string username) {
     SqlConnection conn = new SqlConnection();
     conn.ConnectionString = @"Data Source = .\sqlexpress;" +
                    "Database =Test ; Integrated Security=True";
 
     string sql = "INSERT INTO Test (name, username)"+"values(@name,@lname)";
 
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlParameter[] param = new SqlParameter[7];
            //param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
            // new SqlParameter("@ Full Name", param[0].SqlDbType.VarChar, 50);
            param[0] = new SqlParameter("@name", System.Data.SqlDbType.VarChar, 50);
            param[1] = new SqlParameter("@Uusername",System.Data.SqlDbType.VarChar,50);
            param[0].Value = username;
            param[1].Value = username;
            for (int i = 0; i < param.Length; i++)
            {
                cmd.Parameters.Add(param[i]);
                cmd.Parameters.Add("@name", System.Data.SqlDbType.VarChar).Value = txtfname;
                cmd.Parameters.Add("@Uusername", System.Data.SqlDbType.VarChar).Value = txtlname;
            }
 
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            conn.Close();
        }
    }                                                                
 protected void  sub_Click(object sender, EventArgs e)
{
            //call the method to execute insert to the database
            ExecuteInsert(txtfname.Text, 
                          txtlname.Text);
            Response.Write("Record was successfully added!");
            ClearControls(Page);
        }
  public static void ClearControls(Control Parent){
 
        if (Parent is TextBox)
        { (Parent as TextBox).Text = string.Empty; }
        else
        {
            foreach (Control c in Parent.Controls)
                ClearControls(c);
        }
}      
}


错误消息

它正在产生以下错误msg
在system.Data.Dll中发生类型为"System.ArguementsNullException"的异常,但未在用户代码中处理.


ERROR Msg

it is gennerating the following error msg
An Exception of type ''System.ArguementsNullException''occured in system.Data.Dll but was not handled in user code.

推荐答案

这是引发的异常当将空引用传递给不接受为有效参数的方法时.

参考: MSDN:ArgumentNullException类 [
This is the exception that is thrown when a null reference is passed to a method that does not accept it as a valid argument.

Refer: MSDN: ArgumentNullException Class[^]

Putting a DEBUGGER and stepping into code using F11 would help.


On another note, do you have nay idea on what you have written here:
SqlParameter[] param = new SqlParameter[7];
            //param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
            // new SqlParameter("@ Full Name", param[0].SqlDbType.VarChar, 50);
            param[0] = new SqlParameter("@name", System.Data.SqlDbType.VarChar, 50);
            param[1] = new SqlParameter("@Uusername",System.Data.SqlDbType.VarChar,50);
            param[0].Value = username;
            param[1].Value = username;
            for (int i = 0; i < param.Length; i++)
            {
                cmd.Parameters.Add(param[i]);
                cmd.Parameters.Add("@name", System.Data.SqlDbType.VarChar).Value = txtfname;
                cmd.Parameters.Add("@Uusername", System.Data.SqlDbType.VarChar).Value = txtlname;
            }


上面的代码添加了7 * 3 = 21个参数!我怀疑你甚至想要7个!


Above code is adding 7*3=21 parameters! I doubt you even want 7!


这篇关于我正在尝试从aql数据库中的asp.net psge注册来注册用户,这是我在下面编写代码时遇到的错误msg.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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