2016-05-09 87 views
0

我收到錯誤 沒有從對象類型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

回答

0

錯誤來自此行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); 
相關問題