1
Answer

i had prblm to setting alert for listbox in windows forms

Zakir Ahamed

Zakir Ahamed

7y
169
1
hi friends ,
 
i had a problem to setting a listbox alert error provider problem..
 
my code is:
 
if (btnsave.Text == "SAVE")
{
Hashtable hstbl = new Hashtable();
hstbl.Add("@status", "insert");
hstbl.Add("@ProductNo", txtproduct.Text.Trim());
hstbl.Add("@ProductName", txtpname.Text.Trim());
// hstbl.Add("@BarcodeNo", txtbarcode.Text.Trim());
hstbl.Add("@InternalNo", txtinternal.Text.Trim());
if (string.IsNullOrEmpty(cbbrand.Text))
{
errorProvider7.SetError(cbbrand, "Please enter Product Price");
hstbl.Add("@BrandId", cbbrand.SelectedValue);
return;
}
else
{
errorProvider7.SetError(cbbrand, string.Empty);
}
if (string.IsNullOrEmpty(cbunit.Text))
{
errorProvider8.SetError(cbbrand, "Please enter Product Price");
hstbl.Add("@Unit", cbunit.SelectedValue);
return;
}
else
{
errorProvider8.SetError(cbbrand, string.Empty);
}
hstbl.Add("@ReceivedQuantity", txtquantity.Text.Trim());
if (string.IsNullOrEmpty(cbcategory.Text))
{
errorProvider9.SetError(cbbrand, "Please enter Product Price");
hstbl.Add("@CategoryId", cbcategory.SelectedValue);
return;
}
else
{
errorProvider9.SetError(cbbrand, string.Empty);
}
if (string.IsNullOrEmpty(cblevel1.Text))
{
errorProvider10.SetError(cbbrand, "Please enter Product Price");
hstbl.Add("@SubCategoryId", cblevel1.SelectedValue);
return;
}
else
{
errorProvider10.SetError(cbbrand, string.Empty);
}
if (string.IsNullOrEmpty(cblevel2.Text))
{
errorProvider11.SetError(cbbrand, "Please enter Product Price");
hstbl.Add("@SubsSubCategoryId", cblevel2.SelectedValue);
return;
}
else
{
errorProvider11.SetError(cbbrand, string.Empty);
}
hstbl.Add("@WarehousePrice", txtwsprice.Text.Trim());
hstbl.Add("@ResalePrice", txtrsprice.Text.Trim());
hstbl.Add("@fprice", txtfsprice.Text.Trim());
hstbl.Add("@price", txtprice.Text.Trim());
hstbl.Add("@createdby", Global.UserID);
Int64 intidentity = DataAccessLayer.ExecuteCommand_RowsAffected("Product_Mst_sp", hstbl);
if (intidentity >= 0)
{
MessageBox.Show("Product Details Saved Sucessfully", "Product Alert");
// clear();
//bind();
loadproduct();
//bindgrid();
//bind();
}
 
how i will set a alert with error or any other option ..
 
because of with out alert my db throwing error message duplicate null values is not allowed..
 
Answers (1)
0
Susovan Biswas

Susovan Biswas

NA 10 0 9y
Logic:- 
 
Use HttpFileCollection and System.IO  classes.
 
In the database, the field which saves the image should be of varbinary datatype. 
 
In your Page Load  Event iterate through each HttpPostedFile in the HttpFileCollection class.
Save it temporarily in a folder. Access the save file in a FileStream. Take a byte variable and read the bytes of the declared FileStream into the byte variable. 
 
Send the byte to the database using Data Command Object with the help of Parameters.Add() .Value function. 
 
On successfull saving in database,  delete the file save in the temporary folder.
 
Code Help :-
 
HttpFileCollection  hfc = HttpContext.Current.Request.Files;
if(hfc.Count > 0)
{
   for(int i = 0; i< hfc.count; i++)
      {
          HttpPostedFile hpf = hfc[i];
          string path = HttpContext.Current.Server.MapPath("Path to save the http posted file"); 
         string imageName = hpf.FileName; 
         InsertPic(imageName);
      } 
 
 
private void InsertPic(string imagename)
{
   try
   {
      string path = HttpContext.Current.Server.MapPath("Path where you saved the http posted file");
      FileStream fsImage = new FileStream(path + @imagename, FileMode.Open, FileAccess.Read);
         //a byte array to read the image
         byte[] imgdata = new byte[fsImage.Length];
         fsImage.Read(imgdata, 0, System.Convert.ToInt32(fsImage.Length));
         string qry = "INSERT INTO TableName(imageField)VALUES(@imageField)";
         SqlCommand cmd = new SqlCommand(qry, SqlConnection);
         cmd.Parameters.Add("@imageField", SqlDbType.VarBinary).Value = imgdata;
          SqlConnection.Open();
         cmd.ExecuteNonQuery(); 
         fsImage.Dispose();
         System.IO.File.Delete(HttpContext.Current.Server.MapPath("Path where you saved the http posted file") + imagename);
      }
     catch (Exception ex)
     {
     }
      finally
      {
           SqlConnection.Close();
      } 
}
 
______________________________________________________________________________ 
 Worked for me.