David Turner David Turner - 11 months ago 34
Vb.net Question

Getting error trying to upload data into my database

I am getting the error
No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.

when trying to upload data from a input form into my database. Heres my code if anyone thinks they can help.

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))

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);
//then save it to the Folder
// myinfo.Text = "file " + imgPath + " uploaded";

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

// myConnection.ConnectionString is now set to connectionString.

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





The error is coming from this line myCommand.Parameters.AddWithValue("@number", unumber);. unumber is a textbox and you are extracting the value from the textbox to an int variable number. So you should use that number variable for the param value.

The following lines should be corrected

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