我收到错误 从对象类型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();
}
}
答案 0 :(得分: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);