在将数据添加到表中时,在com.ExecuteNonQuery()处获取错误

时间:2017-03-05 22:28:26

标签: c# mysql asp.net visual-studio webforms

我正在尝试在visual studio 2015(社区版)中保存网络表单中的数据。我一再得到错误:

no mapping exists from object type. Error at line "com.ExecuteNonQuery()".

我在本论坛中尝试过各种解决方案,但它们都不适用于我。请帮忙。谢谢。

错误讯息:

我的代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            string checkuser = "select count(*) from [Table] where Username='" + un.Text + "'";
            SqlCommand com = new SqlCommand(checkuser, conn);
            temp = Convert.ToInt32(com.ExecuteScalar().ToString());
            if (temp == 1)
            {
                Response.Write("User already exists");
            }

            conn.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (temp == 0)
        {
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
                conn.Open();
                string insertquery = "insert into [Table] (Designation, Username, Email, [Password]) values (@Designation, @Username, @Email, @Password)";
                SqlCommand com = new SqlCommand(insertquery, conn);
                com.Parameters.AddWithValue("@Designation", dn.SelectedItem.ToString());
                com.Parameters.AddWithValue("@Username", un.Text);
                com.Parameters.AddWithValue("@Email", em.Text);
                com.Parameters.AddWithValue("@Password", pw.Text);
                com.ExecuteNonQuery();
                Response.Redirect("Managers.aspx");
                Response.Write("Registration Successful");
                conn.Close();
            }
            catch (Exception ex)
            {
                Response.Write("error :" + ex.ToString());
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您的错误似乎在这一行:

com.Parameters.AddWithValue("@Designation", dn.SelectedItem.ToString());

AddWithValue执行映射以确定要传递给方法的对象是什么数据类型。那是失败的。

试试这个:

1)com.Parameters.Add("@Designation", SqlDbType.VarChar).Value = dn.SelectedItem.ToString();

2)如果您想继续使用AddWithValue调试来查看dn.SelectedItem.ToString()内的内容

无论如何,最好使用Add代替AddWithValue因为您现在所遇到的典型问题。看一下这篇文章:http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/

答案 1 :(得分:0)

如上所述,似乎由于对象类型转换无效。

主要区别在于使用AddWithValue时的隐式转换。如果您知道执行的SQL查询(存储过程)正在接受int,nvarchar等类型的值,则没有理由在您的代码中重新声明它。

对于复杂类型的场景(例如DateTime,float),我可能会使用Add,因为它更明确但AddWithValue用于更直接的类型场景(Int到Int)。

  protected void Button1_Click(object sender, EventArgs e) {
    if (temp == 0) {
     try {
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
      conn.Open();

      string insertquery = "insert into [Table] (Designation, Username, Email, [Password]) values (@Designation, @Username, @Email, @Password)";

      SqlCommand com = new SqlCommand(insertquery, conn);

      com.Parameters.Add("@Designation", SqlDbType.VarChar).Value = dn.SelectedItem.ToString();
      com.Parameters.Add("@Username", SqlDbType.VarChar).Value = un.Text;
      com.Parameters.Add("@Email", SqlDbType.VarChar).Value = em.Text;
      com.Parameters.Add("@Password", SqlDbType.VarChar).Value = pw.Text;
      com.ExecuteNonQuery();
      Response.Redirect("Managers.aspx");
      Response.Write("Registration Successful");
      conn.Close();
     } catch (Exception ex) {
      Response.Write("error :" + ex.ToString());
     }
    }
   }