Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
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

 
Share this answer
 
You can write following code for uploding image


C#
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;
        
      }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900