使用c#将行插入SQL

时间:2017-03-22 12:26:04

标签: c# asp.net sql-server visual-studio

所以我的问题是,我在将此instert查询写入我的SQL服务器时收到错误消息。我正在使用Visual Studie 2017.错误信息:

  

System.ArgumentException:'对于托管服务提供商的已知本机类型,没有分配对象类型System.Web.UI.HtmlControls.HtmlInputGenericControl。'

这发生在cmd.ExecuteNonQuery();上,但我已尝试ScalarReader。所有人都给出了错误。

我真的希望有人有一个解决方案,这是一个大学项目。

protected void btnRegister_Click(object sender, EventArgs e)
{
    string connString = WebConfigurationManager.ConnectionStrings["csPAD"].ConnectionString;

    SqlConnection myConnection = new SqlConnection(connString);
    if (Page.IsValid)
    {
        string Study = rbStudy.SelectedValue;

        string sqlQuery = "INSERT INTO Registratie ([Firstname], [middlename], [Lastname], [Dateofbirth], [Study], [Email]) VALUES (@Firstname,@middlename,@Lastname,@Dateofbirth,@Study,@Email)";
       SqlCommand cmd = new SqlCommand(sqlQuery, myConnection)
        {

            cmd.Parameters.AddWithValue("@Firstname", txtFirstname.Text);
            if (txtmiddlename.Text == "")
            {
                cmd.Parameters.AddWithValue("@middlename", "");
            }
            else
            {
                cmd.Parameters.AddWithValue("@middlename", txtmiddlename.Text);
            }
            cmd.Parameters.AddWithValue("@Lastname", txtLastname.Text);
            cmd.Parameters.AddWithValue("@Dateofbirth", Dateofbirth);
            cmd.Parameters.AddWithValue("@Study", Study);
            if (txtEmail.Text == "")
            {
                cmd.Parameters.AddWithValue("@Email", "");
            }
            else
            {
                cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
            }        
            myConnection.Open();
            int rows = cmd.ExecuteNonQuery();
            MessageBox.Show(rows + " rows affected!");
            myConnection.Close();                             
        }
    }
    else
    { 
        lblNotification.Text = "Everything is required.";
    }
}

4 个答案:

答案 0 :(得分:1)

对您的Asp.net和C#代码进行以下更改

  

替换

<input type="date" ID="DateofBirth" runat="server" min="01-01-1990" max="01-01-2020">

使用

 <asp:TextBox ID="DateofBirth" runat="server"></ asp:TextBox>

cmd.Parameters.AddWithValue("@Dateofbirth", Dateofbirth.Text);

答案 1 :(得分:0)

@David以下代码位于我的HTML页面中:

<td><asp:Label ID="lblDateofBirth" runat="server" Text="DateofBirth:"></asp:Label></td>
            <td><input type="date" ID="DateofBirth" runat="server" min="01-01-1990" max="01-01-2020"></td>
            <td><asp:RequiredFieldValidator ID="rfvDateofBirth" runat="server" ControlToValidate="DateofBirth" ErrorMessage="Dit veld is vereist!" ForeColor="Red" ></asp:RequiredFieldValidator></td>

这是在我的Web.config中配置的:

<connectionStrings>
    <add name="csPAD" connectionString="Data Source=MARCO-LAPTOP;Initial Catalog=ProjectPAD;Integrated Security=True"/>
  </connectionStrings>

要求提供完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;


public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void btnRegister_Click(object sender, EventArgs e)
        {
            string connString = WebConfigurationManager.ConnectionStrings["csPAD"].ConnectionString;

            SqlConnection myConnection = new SqlConnection(connString);
            if (Page.IsValid)
            {
                string Study = rbStudy.SelectedValue;

                string sqlQuery = "INSERT INTO Registratie ([Firstname], [middlename], [Lastname], [Dateofbirth], [Study], [Email]) VALUES (@Firstname,@middlename,@Lastname,@Dateofbirth,@Study,@Email)";
               SqlCommand cmd = new SqlCommand(sqlQuery, myConnection)
                {

                    cmd.Parameters.AddWithValue("@Firstname", txtFirstname.Text);
                    if (txtmiddlename.Text == "")
                    {
                        cmd.Parameters.AddWithValue("@middlename", "");
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@middlename", txtmiddlename.Text);
                    }
                    cmd.Parameters.AddWithValue("@Lastname", txtLastname.Text);
                    cmd.Parameters.AddWithValue("@Dateofbirth", Dateofbirth);
                    cmd.Parameters.AddWithValue("@Study", Study);
                    if (txtEmail.Text == "")
                    {
                        cmd.Parameters.AddWithValue("@Email", "");
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
                    }        
                    myConnection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    MessageBox.Show(rows + " rows affected!");
                    myConnection.Close();                             
                }
            }
            else
            { 
                lblNotification.Text = "Everything is required.";
            }
        }

}

答案 2 :(得分:0)

你能这样试试吗?

con.Open();
SqlCommand cmd = new SqlCommand(@"insert into tbl_insert values(@name,@email,@add)", con);
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@email", txtemail.Text);
cmd.Parameters.AddWithValue("@add", txtadd.Text);
cmd.ExecuteNonQuery();
con.Close();

答案 3 :(得分:-2)

我的猜测是你需要修复以下行(Dateofbirth似乎是一个控件名?):

 cmd.Parameters.AddWithValue("@Dateofbirth", Dateofbirth);