using System;
using System.IO;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
public partial class AddGoods : System.Web.UI.Page
{
protected void ImgBtn_Greate_Good_Click(object sender, ImageClickEventArgs e)
{
// Check to see if file was uploaded "Проверить, что адрес не пуст"
if (filMyFile.PostedFile != null)
{
// Get a reference to PostedFile object "Получить ссылку на объект"
HttpPostedFile myFile = filMyFile.PostedFile;
// Get size of uploaded file "Получить размер файла"
int nFileLen = myFile.ContentLength;
// make sure the size of the file is > 0 "Удостоверится, что размер файла > 0"
if (nFileLen > 0)
{
// Allocate a buffer for reading of the file "Подготовить буфер для чтения файла"
byte[] myData = new byte[nFileLen];
// Read uploaded file from the Stream "Прочитать загруженый файл из потока"
myFile.InputStream.Read(myData, 0, nFileLen);
// Create a name for the file to store "Создать название файла для того, чтобы хранить"
string strFilename = Path.GetFileName(myFile.FileName);
// Write data into a file "Записать данные в файл"
WriteToFile(Server.MapPath(strFilename), ref myData);
// Set label's text "Текст ярлыка"
Lbl_AddImage.Text =
"Filename: " + strFilename + "<br>" +
"Size: " + nFileLen.ToString() + "<p>";
// Set URL of the the object to point to the file we've just saved
// "Передать путь файла который только что записали"
Img_AddImage.ImageUrl = strFilename;
Img_AddImage.ToolTip = "This file was stored to as file.";
Lbl_AddImage.Text = Img_AddImage.ImageUrl;
// show the images and text "Показать рисунок и текст"
this.Img_AddImage.Visible = true;
//imgDB.Visible = true;
this.Lbl_AddImage.Visible = true;
//lblText2.Visible = true;
// Writes file to the database
//==============================
// Create a byte[] from the input file
int len = filMyFile.PostedFile.ContentLength;
byte[] pic = new byte[len];
filMyFile.PostedFile.InputStream.Position = 0;
filMyFile.PostedFile.InputStream.Read(pic, 0, len);
//return pic;
// Insert the image and comment into the database
string ConnectionString = ConfigurationSettings.AppSettings.Get("ConnectionString");
SqlConnection connection1 = new SqlConnection(ConnectionString);
try
{
connection1.Open();
SqlCommand command1 = new SqlCommand("insert into AddG " + "(name_goods,description_goods,cost_goods,comment,picture) values (@name_goods,@description_goods,@cost_goods,@comment,@picture)\n SELECT SCOPE_IDENTITY()", connection1);
command1.Parameters.AddWithValue("@name_goods", this.TB_NameGood.Text);
command1.Parameters.AddWithValue("@description_goods", this.TB_Description.Text);
command1.Parameters.AddWithValue("@cost_goods", this.TB_Cost.Text);
command1.Parameters.AddWithValue("@comment", this.TB_Comment.Text);
command1.Parameters.AddWithValue("@picture", pic);
SqlDataReader adapter;
adapter = command1.ExecuteReader();
}
finally
{
connection1.Close();
this.Lbl_info.Visible = true;
this.Lbl_info.Text = "Thanks, the goods are added. For entering the new goods press New Good";
}
}
}
}
// Writes file to current folder "Записать файл в текущую папку"
private void WriteToFile(string strPath, ref byte[] Buffer)
{
// Create a file "Создать файл"
FileStream newFile = new FileStream(strPath, FileMode.Create);
// Write data to the file "Записать данные к файлу"
newFile.Write(Buffer, 0, Buffer.Length);
// Close file "Закрыть файл"
newFile.Close();
}
// Reads the name of current web page
private string GetMyName()
{
// Get the script name
string strScript = Request.ServerVariables["SCRIPT_NAME"];
// Get position of last slash
int nPos = strScript.LastIndexOf("/");
// Get everything after slash
if (nPos > -1)
strScript = strScript.Substring(nPos + 1);
return strScript;
}
protected void Btn_New_Goods_Click(object sender, EventArgs e)
{
this.TB_NameGood.Text = "";
this.TB_Description.Text = "";
this.TB_Cost.Text = "";
this.TB_Comment.Text = "";
this.Img_AddImage.Visible = false;
this.Img_AddImage.ImageUrl = "";
this.Lbl_AddImage.Text = "";
this.Lbl_info.Text = "";
}
}