尝试上传数据到我的数据库时出错

问题描述:

我收到错误 没有从对象类型System.Web.UI.WebControls.TextBox到已知托管提供程序本机类型的映射。尝试上传数据到我的数据库时出错

当试图从输入表单上载数据到我的数据库。如果有人认为他们可以提供帮助,请查看我的代码

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.IO; 

public partial class register : System.Web.UI.Page 

{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     string elmtreeconnect = WebConfigurationManager.ConnectionStrings["elmtreeconnect"].ConnectionString; 
     SqlConnection myConnection = new SqlConnection(elmtreeconnect); 
    } 
    protected void uploadbutton_Click(object sender, EventArgs e) 
    { 

     string UpPath = Server.MapPath("~/files/"); 

     Random r = new Random(); 
     int rInt = r.Next(0, 10000); 

     if (!Directory.Exists(UpPath)) 
     { 
      Directory.CreateDirectory(UpPath); 

     } 
     else 
     { 
      int imgSize = myimage.PostedFile.ContentLength; 
      string imgName = myimage.FileName; 
      string imgPath = "~/files/" + rInt + imgName; 

      if (myimage.PostedFile.ContentLength > 1500000) 
      { 
       Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true); 
      } 
      else 
      { 
       //then save it to the Folder 
       myimage.SaveAs(Server.MapPath(imgPath)); 
       // myinfo.Text = "file " + imgPath + " uploaded"; 
      } 


     } 
     string connectionString = WebConfigurationManager.ConnectionStrings["elmtreeconnect"].ConnectionString; 
     SqlConnection myConnection = new SqlConnection(connectionString); 

     // myConnection.ConnectionString is now set to connectionString. 
     myConnection.Open(); 

     string img = rInt + myimage.FileName; 
     string passworddata = upassword.Text; 
     string usernamedata = uname.Text; 
     string addressdata = uadd.Text; 
     string emaildata = ueadd.Text; 
     string instdata = ulearning.Text; 
     int userleveldata = Convert.ToInt16(typeID.SelectedValue); 
     int number = Convert.ToInt32(unumber.Text); 

     string query = "INSERT INTO user(uname, pword, address, email, phone, image, learning, typeID) VALUES(@name, @pass, @add, @email, @number, @image, @inst, @type)"; 

     SqlCommand myCommand = new SqlCommand(query, myConnection); 

     //create a parameterised object 
     myCommand.Parameters.AddWithValue("@name", usernamedata); 
     myCommand.Parameters.AddWithValue("@pass", passworddata); 
     myCommand.Parameters.AddWithValue("@add", addressdata); 
     myCommand.Parameters.AddWithValue("@email", emaildata); 
     myCommand.Parameters.AddWithValue("@number", unumber); 
     myCommand.Parameters.AddWithValue("@img", myimage); 
     myCommand.Parameters.AddWithValue("@inst", ulearning); 
     myCommand.Parameters.AddWithValue("@type", userleveldata); 


     myCommand.ExecuteNonQuery(); 




     myConnection.Close(); 



    } 
} 
+2

**不要明文存储密码** – SLaks

错误来自此行myCommand.Parameters.AddWithValue("@number", unumber);。 unumber是一个文本框,并且您正在将文本框中的值提取到一个int变量编号。所以你应该使用该数字变量作为参数值。

以下行应该予以纠正

myCommand.Parameters.AddWithValue("@number", unumber); 
myCommand.Parameters.AddWithValue("@img", myimage); 
myCommand.Parameters.AddWithValue("@inst", ulearning); 

myCommand.Parameters.AddWithValue("@number", number); 
myCommand.Parameters.AddWithValue("@img", img); 
myCommand.Parameters.AddWithValue("@inst", instdata);