2017-03-22 61 views
-2

所以我的問題是,我在寫入這個instert查詢到我的SQL服務器時收到一條錯誤消息。我使用Visual Studie 2017年的錯誤消息:用c插入行到SQL#

System.ArgumentException:「沒有分配對象類型System.Web.UI.HtmlControls.HtmlInputGenericControl到已知原始類型的託管服務提供商。」

這happends我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

什麼是Dateofbirth對象? – David

+1

附註:'middlename'和'Email'的if語句是毫無意義的 – TheLethalCoder

+0

'connString'是否有正確的值? – TheLethalCoder

回答

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> 

AND

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

我的猜測是,你需要解決以下行(出生日期似乎是一個控制的名字嗎?):

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

'研究'的類型'字符串'可以在代碼中看到。 –

+0

對不起,意思是出生日期(剛剛修復) –

0

@大衛下面的代碼是在我的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."; 
      } 
     } 

} 
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();