I’m doing an user profile, first the user selects the picture and upload into a folder with this code, the image is displayed after is uploaded:
protected void btnUpload_Click(object sender, EventArgs e)
{
// Initialize variables
string sSavePath;
string sThumbExtension;
int intThumbWidth;
int intThumbHeight;
// Set constant values
sSavePath = "images/";
sThumbExtension = "_thumb";
intThumbWidth = 160;
intThumbHeight = 120;
// If file field isn’t empty
if (filUpload.PostedFile != null)
{
// Check file size (mustn’t be 0)
HttpPostedFile myFile = filUpload.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
{
lblOutput.Text = "El archivo no fue cargado.";
return;
}
// Check file extension (must be JPG)
if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
{
lblOutput.Text = "El archivo debe tener una extensión JPG";
return;
}
// Read file into a data stream
byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
// Make sure a duplicate file doesn’t exist. If it does, keep on appending an
// incremental numeric until it is unique
string sFilename = System.IO.Path.GetFileName(myFile.FileName);
int file_append = 0;
while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
{
file_append++;
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ file_append.ToString() + ".jpg";
}
// Save the stream to disk
System.IO.FileStream newFile
= new System.IO.FileStream(Server.MapPath(sSavePath + sFilename),
System.IO.FileMode.Create);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
// Check whether the file is really a JPEG by opening it
System.Drawing.Image.GetThumbnailImageAbort myCallBack =
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap;
try
{
myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename));
// If jpg file is a jpeg, create a thumbnail filename that is unique.
file_append = 0;
string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ sThumbExtension + ".jpg";
while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile)))
{
file_append++;
sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
file_append.ToString() + sThumbExtension + ".jpg";
}
// Save thumbnail and output it onto the webpage
System.Drawing.Image myThumbnail
= myBitmap.GetThumbnailImage(intThumbWidth,
intThumbHeight, myCallBack, IntPtr.Zero);
myThumbnail.Save(Server.MapPath(sSavePath + sThumbFile));
imgPicture.ImageUrl = sSavePath + sThumbFile;
// Displaying success information
lblOutput.Text = "El archivo fue cargado con exito!";
// Destroy objects
myThumbnail.Dispose();
myBitmap.Dispose();
}
catch (ArgumentException errArgument)
{
// The file wasn't a valid jpg file
lblOutput.Text = "No es un archivo .jpg valido";
System.IO.File.Delete(Server.MapPath(sSavePath + sFilename));
}
}
}
After that the user finish with the other fields of the profile (name, email, etc), there is a save button and is saved into the database with this:
This is the front code
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pruebaConnectionString %>"
InsertCommand="INSERT INTO Curriculum(Nombre, Correo) VALUES (@TextBox1, @TextBox2)">
<InsertParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="" Name="TextBox1" PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox2" DefaultValue="" Name="TextBox2" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
Actually there are more fields, but to make it short i just copy the first 2, the nombre field is the only that cannot be null on the database
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.Insert();
String strConn = "Data Source=TOSHI;Initial Catalog=prueba;Integrated Security=True";
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
string strQuery = "Insert into curriculum (imagen) values (@imgPicture)";
cmd.CommandText = strQuery;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@imgPicture", (imgPicture.ImageUrl == null ? (object)DBNull.Value : (object)imgPicture.ImageUrl));
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
what now i’m trying to do is that when the user clicks the save button (or what is on the method button1_click), the image url is saved into the database on the field Imagen that is a varchar 50, but is not working, i get: Cannot insert the value NULL into column ‘Nombre’, table ‘prueba.dbo.Curriculum’; column does not allow nulls. INSERT fails.
The statement has been terminated.
But if i leave the button1_click method with just SqlDataSource1.Insert(); the fields get saved onto the database.
Any idea how to save the image url onto the database? Hope i’m being clear with my explanation!
Thanks! 😀
Are you trying to create a new record (in which canse you need to specify all of the values) or are you tring to update a record?
At the moment your code is trying to do an INSERT which will create a new record in the curriculum table, but you are only setting the 1 value. Maybe you want to do an UPDATE instead?