codice:
void Page_Load(object sender, System.EventArgs e)
{
titletext2.Text = "INSERISCI ARTICOLO";
Status.Text += "";
string dbconn, sql_grand, sql, sql2, sql_categoria, sql_categoria2, dbcomm, dbread;// sql_on_line
OleDbConnection connDB;
OleDbCommand cmd;
OleDbDataReader aReader;
OleDbCommand cmd2;
OleDbDataReader aReader2;
OleDbCommand cmd_categoria;
OleDbDataReader aReader_categoria;
OleDbCommand cmd_categoria2;
OleDbDataReader aReader_categoria2;
string connString = ConfigurationSettings.AppSettings["dbConnString"];
connDB = new OleDbConnection(connString);
connDB.Open();
sql_categoria = "SELECT DISTINCT CODICE_CATEGORIA FROM TabCategoria ORDER BY CODICE_CATEGORIA";
cmd_categoria = new OleDbCommand(sql_categoria, connDB);
aReader_categoria = cmd_categoria.ExecuteReader();
if (!Page.IsPostBack)
{
ArrayList values = new ArrayList();
try
{
values.Add("");
while (aReader_categoria.Read())
{
values.Add(aReader_categoria[0]);
}
codice_categoria.DataSource = values;
codice_categoria.DataBind();
}
finally
{
aReader_categoria.Close();
}
}
sql_categoria2 = "SELECT DISTINCT CODICE_CATEGORIA FROM TabCategoria ORDER BY CODICE_CATEGORIA";
cmd_categoria2 = new OleDbCommand(sql_categoria2, connDB);
aReader_categoria2 = cmd_categoria2.ExecuteReader();
if (!Page.IsPostBack)
{
ArrayList values = new ArrayList();
try
{
values.Add("");
while (aReader_categoria2.Read())
{
values.Add(aReader_categoria2[0]);
}
correlati.DataSource = values;
correlati.DataBind();
}
finally
{
aReader_categoria2.Close();
}
}
sql2 = "SELECT DISTINCT CODICE_LINGUA FROM TabLingua ORDER BY CODICE_LINGUA";
cmd2 = new OleDbCommand(sql2, connDB);
aReader2 = cmd2.ExecuteReader();
if (!Page.IsPostBack)
{
ArrayList language_values = new ArrayList();
try
{
language_values.Add("");
while (aReader2.Read())
{
language_values.Add(aReader2[0]);
}
codice_lingua.DataSource = language_values;
codice_lingua.DataBind();
}
finally
{
aReader2.Close();
connDB.Close();
}
}
}
bool IsImage(string fileName)
{
string ext = Path.GetExtension(fileName).ToLower();
bool imageFlag = false;
if (ext != null)
{
switch (ext)
{
case ".emf" :
case ".exif" :
case ".ico" :
case ".wmf" :
case ".png" :
case ".gif" :
case ".bmp" :
case ".tiff" :
case ".tif" :
case ".jpeg" :
case ".jpg" : imageFlag = true; break;
default: imageFlag = false; break;
}
}
else
imageFlag = false;
return imageFlag;
}
void CalcolaAspectRatio_BestFit(double orgWidth, double orgHeight, ref double newWidth, ref double newHeight)
{
if ((orgWidth == newWidth) && (orgHeight == newHeight)) return;
if ((newWidth > orgWidth) && (newHeight > orgHeight))
{
newWidth = orgWidth;
newHeight = orgHeight;
return;
}
double aw = orgWidth / orgHeight;
double ah = orgHeight / orgWidth;
double resw = aw * newHeight;
double resh = ah * newWidth;
if (resw > newWidth)
{
resw = newWidth;
resh = ah * resw;
}
if (resh > newHeight)
{
resh = newHeight;
resw = aw * resh;
}
newWidth = resw;
newHeight = resh;
}
void CalcolaAspectRatio_Biggest(double orgWidth, double orgHeight, ref double newWidth, ref double newHeight)
{
if ((orgWidth == newWidth) && (orgHeight == newHeight)) return;
if ((newWidth > orgWidth) && (newHeight > orgHeight))
{
newWidth = orgWidth;
newHeight = orgHeight;
return;
}
double resw = newWidth;
double resh = newHeight;
double aw = orgWidth / orgHeight;
double ah = orgHeight / orgWidth;
if (resw > orgWidth) resw = orgWidth;
if (resh > orgHeight) resh = orgHeight;
if (orgWidth != orgHeight)
{
if (orgWidth > orgHeight)
{
resw = resw;
resh = ah * resw;
}
else
{
if (orgWidth < orgHeight)
{
resh = resh;
resw = aw * resh;
}
}
}
newWidth = resw;
newHeight = resh;
}
string giveMeANewName(string orgName, string pathToCheck)
{
bool exit = false;
string path = pathToCheck + "/";
string fileName = Path.GetFileNameWithoutExtension(orgName);
string ext = Path.GetExtension(orgName).ToLower();
int count = 0;
string newFileName = "";
path = (path.Replace("\\", "/")).Replace("//", "/");
path = Server.MapPath(path);
do
{
newFileName = fileName + count + ext;
if (!File.Exists(path + newFileName)) exit = true;
count ++;
} while(!exit);
return (newFileName);
}
string ResizeAnUploadedImage(HttpPostedFile file, double newWidth, double newHeight, string virPath, bool overwrite)
{
string destPath = ".";
double resWidth = 1.0;
double resHeight = 1.0;
bool owr = false;
bool exist = false;
if (virPath != "") destPath = virPath;
if (newWidth > 0) resWidth = newWidth;
if (newHeight > 0) resHeight = newHeight;
if (overwrite)
owr = true;
else
owr = false;
int pathCheck = (destPath.Substring(destPath.LastIndexOf('/') + 1)).Length;
if (pathCheck > 0) destPath += "/";
string fileName = Path.GetFileName(file.FileName);
string estensione = Path.GetExtension(file.FileName).ToLower();
ImageCodecInfo[] imgCodec = ImageCodecInfo.GetImageEncoders();
EncoderParameters encPars = new EncoderParameters(2);
EncoderParameter encPar1 = new EncoderParameter( System.Drawing.Imaging.Encoder.Quality, 100L );
EncoderParameter encPar2 = new EncoderParameter( System.Drawing.Imaging.Encoder.Compression, 100L );
encPars.Param[0] = encPar1;
encPars.Param[1] = encPar2;
destPath = Server.MapPath(destPath);
exist = File.Exists(destPath + fileName);
if (!owr && exist)
{
fileName = giveMeANewName(file.FileName, virPath);
exist = false;
}
if (!exist && (fileName.Length != 0) || owr)
{
if (!IsImage(fileName))
{
if (fileName.Length != 0)
Status.Text += "<span style=\"color:Red;\">File '" + fileName + "' is not a valid image format. Upload... Aborted.</span>
";
return ("");
}
using(System.Drawing.Image image = System.Drawing.Image.FromStream(file.InputStream))
{
CalcolaAspectRatio_Biggest(image.Width, image.Height, ref resWidth, ref resHeight);
using(Bitmap bitmap = new Bitmap((int)(resWidth), (int)(resHeight)))
{
string message = "Uploaded";
bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics g = Graphics.FromImage(bitmap);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(image, new Rectangle(0, 0, (int)(resWidth), (int)(resHeight)));
g.Dispose();
if (exist && owr)
{
message = "OverWritted";
try
{
File.Delete(destPath + fileName);
}
catch(IOException ioe)
{
message = "<div id=\"error\">Error: " + ioe.GetType().Name + ": The write operation could not be performed because the specified part of the file is locked. Not OverWritted</div>";
}
}
if ((estensione == ".jpg") || (estensione == ".jpeg"))
bitmap.Save(destPath + fileName, imgCodec[1], encPars);
else
bitmap.Save(destPath + fileName, image.RawFormat);
if (fileName == file.FileName)
Status.Text += "<span style=\"color:Green;\">File '" + fileName + " @ [" + (int)(image.Width) + "px X " + (int)(image.Height) + "px]' " + message + " with this new size [" + (int)(resWidth) + "px X " + (int)(resHeight) + "px]</span>
";
else
Status.Text += "<span style=\"color:Green;\">File \"<u>" + file.FileName + "</u>\" saved &renamed in \"" + fileName + "\".</span>
";
}
}
}
return (fileName);
}
string imgDestFolder()
{
return ("/public/image/foto/");
}
string tbnDestFolder()
{
return ("/public/image/miniature/");
}
void InserisciNomiDeiFilesNelDB_Access(string fileList, char token, string codice_categoria, string codice_lingua, string on_line, string nome, string codice_articolo,string descrizione_corta, string descrizione, string prezzo, string iva, string prezzo_acquisto, string prezzo_vendita, string prezzo_ingrosso, string punti_sconto, string offerta, string stato, string data_inserimento, string giacenza, string giacenza_infinita, string produttore, string taglia, string peso, string misure, string correlati, string nome_comune, string nome_scientifico, string nome_famiglia)
{
string [] fileListSplitted = fileList.Split(new Char [] {token});
int fLS = fileListSplitted.Length;
Status.Text += "
<hr>Inserimento dei dati nel DataBase :
";
string connString = ConfigurationSettings.AppSettings["dbConnString"];
string imgFields = "";
string imgValues = "";
int ii = 0;
foreach (string s in fileListSplitted)
{
if (s != "")
{
ii++;
imgFields+="IMG_"+ii.ToString()+",IMG_TBN_"+ii.ToString()+",";
imgValues+="'" + (imgDestFolder() + s) + "','" + (tbnDestFolder() + s) + "',";
}
}
string sqlString = "INSERT INTO [TabArticolo] ("+imgFields+"CODICE_CATEGORIA, CODICE_LINGUA, ON_LINE, NOME, CODICE_ARTICOLO, DESCRIZIONE_CORTA, DESCRIZIONE, PREZZO, IVA, PREZZO_ACQUISTO, PREZZO_VENDITA, PREZZO_INGROSSO, PUNTI_SCONTO, OFFERTA, STATO, DATA_INSERIMENTO, GIACENZA, GIACENZA_INFINITA, PRODUTTORE, TAGLIA, PESO, MISURE, CORRELATI, NOME_COMUNE, NOME_SCIENTIFICO, NOME_FAMIGLIA) VALUES ("+imgValues+"'" + codice_categoria + "', '" + codice_lingua + "', '" + on_line + "', '" + nome + "', '" + codice_articolo + "', '" + descrizione_corta + "', '" + descrizione + "', '" + prezzo + "', '" + iva + "', '" + prezzo_acquisto + "', '" + prezzo_vendita + "', '" + prezzo_ingrosso + "', '" + punti_sconto + "', '" + offerta + "', '" + stato + "', '" + data_inserimento + "', '" + giacenza + "', '" + giacenza_infinita + "', '" + produttore + "', '" + taglia + "', '" + peso + "', '" + misure + "','" + correlati + "','" + nome_comune + "','" + nome_scientifico + "', '" + nome_famiglia + "');";
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connString))
{
conn.Open();
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sqlString, conn);
try
{
cmd.ExecuteNonQuery();
Status.Text += "<span style=\"color:Green;\">Record Inserito correttamente. Ok!
</span>";
}
catch (Exception ex)
{
Status.Text += "<span style=\"color:Red;\">Errore : " + ex.Message + "</span>
";
}
finally
{
conn.Close();
}
}
}