Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
try
           {
               string filename = Path.GetFileName(img_upload1.PostedFile.FileName);

               img_upload1.SaveAs(Server.MapPath("~/Content/Img/cars/" + filename));

               string cs = ConfigurationManager.ConnectionStrings["cs_ki"].ToString();
               SqlConnection con = new SqlConnection(cs);
               con.Open();

               SqlCommand cmd = new SqlCommand("insert into tbl_car "
                 + "(cars_brand,cars_type,cars_img1) values (@cars_brand,@cars_type,@cars_img1)", con);
               cmd.Parameters.AddWithValue("@cars_img1", "~/Content/Img/cars/" + filename);

               cmd.Parameters.AddWithValue("@cars_brand", txt_brand.Text);
                cmd.Parameters.AddWithValue("@cars_type", txt_type.Text);
      
               //cmd.Parameters.AddWithValue("@cars_img1", txt_brand.Text);

               cmd.Connection = con;
               cmd.ExecuteNonQuery();




               lbl_alert.Visible = true;
               lbl_alert.Text = 
               con.Close();
           }


What I have tried:

how to insert multiple images for one id?
Posted
Updated 17-Sep-16 10:39am
v2
Comments
Karthik_Mahalingam 17-Sep-16 3:38am    
you will have to create another table to add the image path relevant to the ID.
Aiza Pro 17-Sep-16 7:26am    
so thanks. i create new tbl . now insert imges in one record?
Karthik_Mahalingam 17-Sep-16 7:52am    
insert the primary details in the main table and the images information in the child table.
Aiza Pro 17-Sep-16 10:20am    
SELECT dbo.tbl_car.name, dbo.tbl_img.imgpath
FROM dbo.tbl_car INNER JOIN
dbo.tbl_img ON dbo.tbl_car.id = dbo.tbl_img.carid



Only one of the selected image is stored
Karthik_Mahalingam 17-Sep-16 10:49am    
if there is only one record then only one will be selected.

try like this

SqlConnection con = new SqlConnection();
           con.Open();
           SqlCommand cmd = new SqlCommand("insert into tbl_car (name) values (@name); select scope_identity()", con);
           cmd.Parameters.AddWithValue("@name", txt_name.Text);
           cmd.Connection = con;
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           int id = Convert.ToInt32(dt.Rows[0][0]);

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

           foreach (var file in uploader.PostedFiles)
           {
               int length = uploader.PostedFile.ContentLength;
               byte[] pic = new byte[length];
               uploader.PostedFile.InputStream.Read(pic, 0, length);

                   string filename = Path.GetFileName(uploader.PostedFile.FileName);
                   uploader.SaveAs(Server.MapPath("~/Insert/Img/" + filename));
                   string cs = ConfigurationManager.ConnectionStrings["theoneConnectionString"].ToString();
                   SqlConnection con = new SqlConnection(cs);
                   con.Open();
                   SqlCommand cmd1 = new SqlCommand("insert into tbl_img (imgpath,imgid) values (@imgpath,@id)", con);
                   cmd1.Parameters.AddWithValue("@imgpath", "~/Insert/Img/" + filename);
                   cmd1.Parameters.AddWithValue("@id", id);
                   cmd1.Connection = con;
                   cmd1.ExecuteNonQuery();

                   con.Close();

           }
 
Share this answer
 
v2
Comments
Aiza Pro 17-Sep-16 15:09pm    
so thanks...
problem

The ConnectionString property has not been initialized.
Karthik_Mahalingam 17-Sep-16 15:10pm    
string cs = ConfigurationManager.ConnectionStrings["theoneConnectionString"].ToString();
SqlConnection con = new SqlConnection(cs);
Add like this
Aiza Pro 17-Sep-16 15:20pm    
Line 63: SqlConnection con = new SqlConnection(cs);
Server Error in '/' Application.
Karthik_Mahalingam 17-Sep-16 15:21pm    
Declare the connection string
Aiza Pro 17-Sep-16 15:36pm    
thank you. Only one of the pictures will be included in a database.
name ~/Insert/Img/Linkedin-icon.png
name ~/Insert/Img/Linkedin-icon.png
It is a bad idea to have multiple images paths are inserted in to a single row on the btl_car. The number of images for each car will never be same. So you better create another table to contain the image file path, which should have the carid as foreign key on it.
 
Share this answer
 
Comments
Aiza Pro 17-Sep-16 7:26am    
so thanks. i create new tbl . now insert imges in one record?
NaibedyaKar 17-Sep-16 11:11am    
Awesome!!!

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