|
|
|
Web(Asp.net,c#) ผมใช้ Tools ของ Devexpress gridview ผมต้องการ Upload file image เก็บข้อมูลเข้า database เป็น path ต้องทำยังไงครับ |
|
|
|
|
|
|
|
ช่วยแนะนำการเขียน c# ให้ Upload file image บันทึก file แบบ path ใน database ผมใช้ database ของ sql
Code (ASP)
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="panContent.ascx.cs" Inherits="AssetAdmin.admMaterial.panContent" %>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:assetConnectionString %>" SelectCommand="SELECT * FROM [material] ORDER BY [m_id]"></asp:SqlDataSource>
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" Width="100%" DataSourceID="SqlDataSource1"
KeyFieldName="m_id" OnRowInserting="grid_RowInserting" OnRowDeleting="grid_RowDeleting" OnRowUpdating="grid_RowUpdating">
<Columns>
<dx:GridViewCommandColumn ShowNewButtonInHeader="true" ShowEditButton="true" ShowDeleteButton="true" VisibleIndex="0" />
<dx:GridViewDataTextColumn FieldName="m_id" Caption="รหัส" VisibleIndex="1" />
<dx:GridViewDataTextColumn FieldName="m_name" Caption="ชื่อครุภัณฑ์" VisibleIndex="2" />
<dx:GridViewDataTextColumn FieldName="m_price" Caption="ราคา" VisibleIndex="3" />
<dx:GridViewDataTextColumn FieldName="m_quantity" Caption="จำนวน" VisibleIndex="4" />
<dx:GridViewDataTextColumn FieldName="m_unit" Caption="หน่วยนับ" VisibleIndex="5" />
<dx:GridViewDataComboBoxColumn FieldName="c_id" Caption="หมวดหมู่" VisibleIndex="6">
<PropertiesComboBox>
<Items>
<dx:ListEditItem Value="A" Text="สำนักงานตึก80" />
<dx:ListEditItem Value="B" Text="กองพัฒ" />
</Items>
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataTextColumn FieldName="m_purchaser" Caption="ผู้จัดซื้อ" VisibleIndex="7" />
<dx:GridViewDataDateColumn FieldName="m_buydate" Caption="วันที่ซื้อ" VisibleIndex="8" />
<dx:GridViewDataTextColumn FieldName="m_buyplace" Caption="สถานที่ซื้อ" VisibleIndex="9" />
<dx:GridViewDataTextColumn FieldName="m_recorder" Caption="ผู้บันทึกข้อมูล" VisibleIndex="10" />
<dx:GridViewDataDateColumn FieldName="m_datesave" Caption="วันที่บันทึก" VisibleIndex="11" />
<dx:GridViewDataTextColumn FieldName="m_address" Caption="ที่อยู่ปัจจุบัน" VisibleIndex="12" />
<dx:GridViewDataComboBoxColumn FieldName="s_id" Caption="สถานะ" VisibleIndex="13">
<PropertiesComboBox>
<Items>
<dx:ListEditItem Value="1" Text="เบิกได้" />
<dx:ListEditItem Value="2" Text="งดเบิก" />
</Items>
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataTextColumn FieldName="m_note" Caption="หมายเหตุ" VisibleIndex="14" />
<!--ส่วนของการ upload file image -->
<dx:GridViewDataBinaryImageColumn FieldName="pic_id" Caption="รูปภาพ" VisibleIndex="15">
<PropertiesBinaryImage ImageHeight="170px" ImageWidth="160px">
<EditingSettings Enabled="true" UploadSettings-UploadValidationSettings-MaxFileSize="4194304" />
</PropertiesBinaryImage>
</dx:GridViewDataBinaryImageColumn>
<!--จบส่วนของการ upload file image -->
</Columns>
<Settings ShowPreview="true" />
<SettingsPager PageSize="10" />
<EditFormLayoutProperties ColCount="3">
<Items>
<dx:GridViewColumnLayoutItem ColumnName="m_id">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_name">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_price">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_quantity">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_unit">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="c_id">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_purchaser">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_buydate">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_buyplace">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_recorder">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_datesave">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_address">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="s_id">
</dx:GridViewColumnLayoutItem>
<dx:GridViewColumnLayoutItem ColumnName="m_note" ColSpan="2">
</dx:GridViewColumnLayoutItem>
<!--ส่วนของการ upload file image -->
<dx:GridViewColumnLayoutItem ColumnName="pic_id" RowSpan="4" ShowCaption="False" HelpText="Maximum fils size is 4MB." Width="180px" />
<!--จบส่วนของการ upload file image -->
<dx:EditModeCommandLayoutItem ColSpan="3" HorizontalAlign="Right" />
</Items>
</EditFormLayoutProperties>
<SettingsPager EnableAdaptivity="true" />
<SettingsSearchPanel Visible="true" />
</dx:ASPxGridView>
Code (C#)
Code (C#)
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web;
namespace AssetAdmin.admMaterial
{
public partial class panContent : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
//Insert
protected void grid_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
if (sender is ASPxGridView s)
{
string colsList = string.Empty; //colsList = "m_id,m_name";
string colsValue = string.Empty; //colsValue = "123345,'Hello'";
foreach (GridViewColumn gc in s.Columns)
{
if (gc is GridViewDataColumn gd)
{
if (string.IsNullOrWhiteSpace(gd.FieldName) == false)
{
var colName = gd.FieldName; //m_name
var colValue = e.NewValues[colName]; //'Hello' //ค่าใหม่
//Convert String,Date
if (colsList != string.Empty)
{
colsList += ",";
}
colsList += colName;
if (colsValue != string.Empty)
{
colsValue += ",";
}
if (colName == "m_id" || colName == "m_name" || colName == "m_price" | colName == "m_quantity" || colName == "m_unit" || colName == "c_id" || colName == "m_purchaser" || colName == "m_buyplace" || colName == "m_recorder" || colName == "m_address" || colName == "s_id" || colName == "m_note" || colName == "pic_id")
{
colValue = "'" + colValue + "'";
}
else if (colName == "m_buydate" || colName == "m_datesave")
{
if (colValue is DateTime colDate)//เก็บเวลาปัจจุบัน
{
colValue = "Convert(datetime, '" + colDate.Year + "-" + colDate.Month + "-" + colDate.Day + "')";
}
else
{
Console.Write("Con't connect Datetime");
}
}
colsValue += colValue;
}
}
}
string conn = "Data Source=localhost;Initial Catalog=asset;Integrated Security=True;";
using (SqlConnection myCon = new SqlConnection(conn))
{
myCon.Open();
string sql = @"INSERT INTO material (" + colsList + ")values(" + colsValue + ")";
using (SqlCommand cmd = new SqlCommand(sql, myCon))
{
cmd.ExecuteNonQuery();
}
}
e.Cancel = true;
}
}
//Delete
protected void grid_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
{
if (sender is ASPxGridView s)
{
string colsList = string.Empty;
string colsValue = string.Empty;
foreach (GridViewColumn gc in s.Columns)
{
if (gc is GridViewDataColumn gd)
{
if (string.IsNullOrWhiteSpace(gd.FieldName) == false)
{
var m_id = gd.FieldName;
var colValue = e.Values[m_id];//ค่าเก่า
}
}
}
string conn = "Data Source=localhost;Initial Catalog=asset;Integrated Security=True;";
using (SqlConnection myCon = new SqlConnection(conn))
{
myCon.Open();
string sql = @"DELETE FROM material WHERE m_id = " + e.Values["m_id"] + "";
using (SqlCommand cmd = new SqlCommand(sql, myCon))
{
cmd.ExecuteNonQuery();
}
}
e.Cancel = true;
}
}
//Update
protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
if (sender is ASPxGridView s)
{
string colsList = string.Empty;
string colsValue = string.Empty;
foreach (GridViewColumn gc in s.Columns)
{
if (gc is GridViewDataColumn gd)
{
if (string.IsNullOrWhiteSpace(gd.FieldName) == false)
{
var colName = gd.FieldName;
var colValue = e.NewValues[colName];
//Convert String,Date
if (colsList != string.Empty)
{
colsList += ",";
}
colsList += colName;
if (colsValue != string.Empty)
{
colsValue += ",";
}
if (colName == "m_id" || colName == "m_name" || colName == "m_price" | colName == "m_quantity" || colName == "m_unit" || colName == "c_id" || colName == "m_purchaser" || colName == "m_buyplace" || colName == "m_recorder" || colName == "m_address" || colName == "s_id" || colName == "m_note" || colName == "pic_id")
{
colValue = "'" + colValue + "'";
}
else if (colName == "m_buydate" || colName == "m_datesave")
{
if (colValue is DateTime colDate)//เก็บเวลาปัจจุบัน
{
colValue = "Convert(datetime, '" + colDate.Year + "-" + colDate.Month + "-" + colDate.Day + "')";
}
else
{
Console.Write("Con't connect Datetime");
}
}
colsValue += colValue;
}
}
}
string conn = "Data Source=localhost;Initial Catalog=asset;Integrated Security=True;";
using (SqlConnection myCon = new SqlConnection(conn))
{
myCon.Open();
var m_brydate = string.Empty;
if (e.NewValues["m_buydate"] is DateTime date1)
{
m_brydate = "Convert(datetime, '" + date1.Year + "-" + date1.Month + "-" + date1.Day + "')";
}
else
{
m_brydate = "null";
}
var m_datesave = string.Empty;
if (e.NewValues["m_buydate"] is DateTime date2)
{
m_datesave = "Convert(datetime, '" + date2.Year + "-" + date2.Month + "-" + date2.Day + "')";
}
else
{
m_datesave = "null";
}
string sql = @"UPDATE material SET m_id ='" + e.NewValues["m_id"]
+ "',m_name ='" + e.NewValues["m_name"]
+ "',m_price = '" + e.NewValues["m_price"]
+ "',m_quantity = '" + e.NewValues["m_quantity"]
+ "',m_unit = '" + e.NewValues["m_unit"]
+ "',c_id = '" + e.NewValues["c_id"]
+ "',m_purchaser = '" + e.NewValues["m_purchaser"]
+ "',m_buydate =" + m_brydate
+ ",m_buyplace = '" + e.NewValues["m_buyplace"]
+ "',m_recorder = '" + e.NewValues["m_recorder"]
+ "',m_datesave = " + m_datesave
+ ",m_address = '" + e.NewValues["m_address"]
+ "',s_id = '" + e.NewValues["s_id"]
+ "',m_note = '" + e.NewValues["m_note"]
+ "',pic_id = '" + e.NewValues["pic_id"]
+ "' WHERE m_id = " + e.NewValues["m_id"] + "";
using (SqlCommand cmd = new SqlCommand(sql, myCon))
{
cmd.ExecuteNonQuery();
}
}
e.Cancel = true;
}
}
}
}
database:asset
tablename:picture
Tag : .NET, Web (ASP.NET), C#
|
|
|
|
|
|
Date :
2018-10-11 00:07:39 |
By :
sanchaimax |
View :
1523 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จ้างทำเถอะหลานชาย
|
|
|
|
|
Date :
2020-08-01 14:30:20 |
By :
PhrayaDev |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|