Click here to Skip to main content
14,599,610 members
Rate this:
Please Sign up or sign in to vote.
Hi
I have 5 fields. serial number,category which is a dropdown list, description image upload field and cost price.

I want to add serial number,category description cost price from text box and image to be uploaded from fileupload control and get stored in database in sql server and shows in folder in visual studio.

i have written the code but its not working.

i have made serial number isidentity property to yes.

so for this at runtime fields are getting moved by 1 row. in place of category 1 getting inserted, in place of description category value getting inserted and so on and so forth.

I am sending the code.

Can anyone check and tell me please.stuck here for a long time.

Thanks In advance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
public partial class AddEarRing : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
 
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
 
SqlCommand cmd = new SqlCommand("insert into invent values('" + txtcode.Text + "', '" + DropDownList1.Text + "', '" + txtdesc.Text + "', '" + TextBox1.Text + "', '" + TextBox4.Text + "', '" + txtprice.Text + "')", con);
 

cmd.ExecuteNonQuery();
con.Close();
 

 
Label8.Visible = true;
Label8.Text = "Your Data Successfully added";
txtcode.Text = "";
 
txtdesc.Text = "";
TextBox1.Text = "";
TextBox4.Text = "";
txtprice.Text = "";
}
 
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string path = Server.MapPath("Images/");
 
if (FileUpload1.HasFile)
{
string ext = Path.GetExtension(FileUpload1.FileName);
if (ext == ".jpg" || ext == ".png" || ext == ".jpg")
{
FileUpload1.SaveAs(path + FileUpload1.FileName);
string name = "~/Images/" + FileUpload1.FileName;
 
string s = ("insert into invent values('" + TextBox4.Text + "'");
SqlCommand cmd = new SqlCommand(s, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Label9.Text = "Your file has been uploaded";
 
}
else
{
Response.Write("Please select a file");
}
}
}
}
Posted
Updated 23-Nov-14 20:22pm
v2
Comments
Thanks7872 24-Nov-14 2:06am
   
Why code is in comment section,not in question area?
sudeshna from bangkok 24-Nov-14 2:23am
   
i have put my code in question area,now can you help me please
Sinisa Hajnal 24-Nov-14 3:54am
   
Why this? You're not using name anywhere?
string name = "~/Images/" + FileUpload1.FileName;

As for the question: is your image uploaded and you're just not seeing it in the table or it fails on the upload and you get the path in the datatable? Also, you're doing one insert for the textboxes (on Button click) and another insert for the image, thus you get two rows for the same form data...
sudeshna from bangkok 24-Nov-14 4:54am
   
hello can you help me please.see the second code that i pasted
sudeshna from bangkok 24-Nov-14 4:08am
   
i cant upload it in database
sudeshna from bangkok 24-Nov-14 4:08am
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Drawing;
using System.Data;

public partial class AddEarRing : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{


}
protected void Button1_Click(object sender, EventArgs e)
{
string fileName = string.Empty;
string filePath = string.Empty;
string getPath = string.Empty;
string pathToStore = string.Empty;

SqlCommand cmd = new SqlCommand("invent", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SerialNumber", txtcode.Text);
cmd.Parameters.AddWithValue("@Category", cmbcat.Text);
cmd.Parameters.AddWithValue("@Description", txtdesc.Text);
cmd.Parameters.AddWithValue("@CostPrice", TextBox1.Text);
cmd.Parameters.AddWithValue("@SellingPrice", TextBox4.Text);
cmd.Parameters.AddWithValue("@SoldPrice", txtprice.Text);

try
{
if (FileUpload1.HasFile)
{
fileName = FileUpload1.FileName;
filePath = Server.MapPath("Images/" + System.Guid.NewGuid() + fileName);
FileUpload1.SaveAs(filePath);
cmd.Parameters.AddWithValue("@Image", fileName);
int getPos = filePath.LastIndexOf("\\");
int len = filePath.Length;
getPath = filePath.Substring(getPos, len - getPos);
pathToStore = getPath.Remove(0, 1);
cmd.Parameters.AddWithValue("@BookPicPath", pathToStore);
}
con.Open();
cmd.ExecuteNonQuery();
Label8.Text = "Data saved successfully";
Label8.ForeColor = Color.Green;
ClearControls();


}
catch (Exception)
{
Label8.Text = "Data could not be saved";
Label8.ForeColor = Color.Red;
}
finally
{
con.Close();
cmd.Dispose();
fileName = null;
filePath = null;
getPath = null;
pathToStore = null;
}

}
private void ClearControls()
{
txtcode.Text = string.Empty;
txtdesc.Text = string.Empty;
txtprice.Text = string.Empty;
cmbcat.Text = string.Empty;
TextBox1.Text = string.Empty;
TextBox4.Text = string.Empty;

txtcode.Focus();
}

}


This is my new code that i changed just now. its running but going to the catch statement
sudeshna from bangkok 24-Nov-14 4:19am
   
no data getting inserted in database.
sudeshna from bangkok 24-Nov-14 4:53am
   
can anyone please help me. where is wrong in this code? i cant understand it.
Sinisa Hajnal 24-Nov-14 6:18am
   
what is the error in the catch block? And please move your code into the question using Improve question, rather then posting it into comments.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can write following code for uploding image


string path = "";
      path = Server.MapPath("./img/");

      if (f1.HasFile)
      {
          path = path + Session["uname"] + ".jpg";
          f1.SaveAs(path);
          string img2;
          img2 = "~/img/" + Session["uname"]+".jpg";
          img1.ImageUrl = img2;
        
      }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100