AddProducts.aspx

 

This facility allows the admin to add products to be displayed to the users when they shop.

He can upload images too.The admin can browse the required image file and can upload it.Once it gets uploaded,the path of the images are stored in the database and is displayed in the relevant webpages.The details are stored in a different table and images in a different table.

 

One important point while uploading images

 

You can upload images from any source whether the image is on your  desktop or any other folder.

When you upload  any image the path of the image gets stored in a database.So, the next time you run the application the application looks into the images table and retrieves the path specifies in the table. Hence,it becomes mandatory that the user does not change the location of the image once uploaded.

 

For simplicity sake I have uploaded all the images in C:\inetpub\wwwroot\prjstart\images folder.All users are requested to do the same.

 

 

 

The Source code is as given below.

 

The save method when you click on OK is given below

 

 

 

private void cmdOK_Click(object sender, System.EventArgs e) // An event is fired when the OK button is clicked.

              {

                     SqlConnection sqlConnection =  null;

                     try                                  

 // This method is for saving the record.

                                                            

 // Before that it makes sure the image is uploaded first.

                     {

                           if(chkimgupload()&& validcontrols())

 

 // The image gets loaded in the session variable which is then initialised to the stored procedure in the UPloadDB method

                           {

                                  sqlConnection = new SqlConnection(@"initial catalog=prjstart;user id=sa;password=;server=localhost");

                                  sqlConnection.Open();

                                  SqlCommand sqlCommand = new SqlCommand("addproducts",sqlConnection);

                                  sqlCommand.CommandType=CommandType.StoredProcedure;

                                  SqlCommandBuilder.DeriveParameters(sqlCommand);          // This ok button is final and inserts the details in the products table by executing stored procedure.

                                  sqlCommand.Parameters["@productID"].Value =0;

                                  sqlCommand.Parameters["@CategoryID"].Value = cmbCategoryName.SelectedItem.Value;

                                  sqlCommand.Parameters["@internalproductsubcategoryid"].Value = cmbSubCategoryName.SelectedItem.Value;

                                  sqlCommand.Parameters["@ModelNumber"].Value = txtProductCode.Text;  //insert the values one by one into the parameters.

                                  sqlCommand.Parameters["@ModelName"].Value = txtProductName.Text;

                                  sqlCommand.Parameters["@UnitCost"].Value = txtunitcost.Text;

                                  sqlCommand.Parameters["@Description"].Value= txtproductdescripton.Text;

                                  sqlCommand.Parameters["@DateAdded"].Value = txtDateAdded.Text;

                    

                                  int i = sqlCommand.ExecuteNonQuery();

              UploadToDB(ref sqlCommand, (int)sqlCommand.Parameters["@productID"].Value);  // At last the upload button is called.

                                  if(i!=0)

                                  {

                                  string strprodid =sqlCommand.Parameters["@productID"].Value.ToString();

                                         String strUrl ="WebForm3.aspx?Mode=Edit" +"&" +"ParentCode=";

                                         strUrl += strprodid;

                                         Response.Redirect(strUrl);

                                  }

                                 

                            }

                     }

                     catch (Exception ex)

                     {

                           Console.WriteLine(ex.Message);

                     }

                     finally

                     {

                           if (sqlConnection != null) sqlConnection.Close();

                     }

              }

 

 

The validation code for this page is

 

private bool chkimgupload()  // This checks for if the image is upladed in the database .Only then u can save the record.

              {

                     Label2.Text=Session["Image"].ToString();

                     if(Label2.Text=="")

                     {

                           RegisterStartupScript("chkup","<script>callme();</script>");

                           return false;

                     }

                     return true;

              }

 

              private bool validcontrols()

              {

                     bool blnStatus = true;

                     string Strerr ="";

                     if(txtProductCode.Text.CompareTo("")==0)

                     {

                           Strerr ="Please enter the product code.\\n";

                           blnStatus = false;

                     }

                     if(txtProductName.Text.CompareTo("")==0)

                     {

                           Strerr +="Please enter the product name.\\n";

                           blnStatus = false;

                     }

                     if(txtunitcost.Text.CompareTo("")==0)

                     {

                           Strerr +="Please enter the unit cost.\\n";

                           blnStatus = false;

                     }

                     if(Strerr.CompareTo("")==1)

                     {

                           RegisterStartupScript("Callerr","<script>callerr('"+ Strerr +"')</script>");

                     }

                     return blnStatus;

              }

 

 

The final upload to the database code is here below

 

private void UploadToDB(ref SqlCommand  sqlCommand, int ProductId)

              {

                     sqlCommand.Parameters.Clear();                                

 

// final upload method called when the last insert is pushed.

                    

sqlCommand.CommandType = CommandType.StoredProcedure;

                     sqlCommand.CommandText = "UploadImages";

                     sqlCommand.CommandType=CommandType.StoredProcedure; 

 

// Initializes  the stored procedure

                     SqlCommandBuilder.DeriveParameters(sqlCommand); 

                     sqlCommand.Parameters["@Image"].Value = Session["Image"]; 

 

//The session is stored in the parameters

                     sqlCommand.Parameters["@ImageName"].Value =lblImage.Text;

                     sqlCommand.Parameters["@ImageType"].Value = lblImageType.Text; 

                     sqlCommand.Parameters["@ProductId"].Value = ProductId;

                     sqlCommand.ExecuteNonQuery();                      // execute non query for executing the stored procedure.

              }

 

 

The code for uploading the image in the database is here

 

// This  reads the image in a session variable

 

 

private void BinaryReadImage()

              {

                     try

                     {

                           selectedFile = File1.PostedFile;        //  the HTML file uploadcontrol  is used on the form                                            /* Code to read the image type and parameters */

                           binaryImagedata = new byte[selectedFile.ContentLength];  //determine the length.

                           selectedFile.InputStream.Read(binaryImagedata,0,selectedFile.ContentLength);

                           Session.Add("Image",binaryImagedata);   //Stored the image in Session unless and until the final ok button is pushed.

                           lblImage.Text = selectedFile.FileName.ToString();

                           lblImageType.Text = selectedFile.ContentType;

                           Message += validation(selectedFile);

                     }

                     catch

                     {

                           Message += "Binary Read Failed<BR>";

                     }

              }

 

 

 

 

 

 

Back to the Main Page