my project is in c sharp
and must upload pictures to MySql DataBase
the uploadfile controle is included in asp form that i made
but the problem is that the uploadfile always get the Postedfile = null
i tried to use html input file but the same problem still
here's my code :
Expand|Select|Wrap|Line Numbers
- <%@ Page Language="C#" MasterPageFile="~/MasterPageB.master" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Add_Pictures" Title="Untitled Page" %>
- <%@ MasterType TypeName="MasterPageB" %>
- <%@ Register assembly="MyFormView" namespace="MyFormView" tagprefix="asp" %>
- <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
- <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderB" Runat="Server">
- <asp:MyForm ID="AddPictureMyForm" runat="server" method="post"
- DataSourceID="picturesqlDataSource" Show_Cancel_button="true"
- DefaultMode="Insert" DataKeyNames="id" Visible="true" Width="75%"
- Height="390px">
- <InsertItemTemplate>
- <asp:Panel ID="InsertPanel" runat="server" BorderStyle="Solid" BorderWidth="2px"
- ScrollBars="Auto" Width="100%" Height="100%">
- <table style="width:657px; height: 233px;" align="center">
- <tr>
- <td align="center" style="font-weight: bold; width: 98px; height: 30px;">
- </td>
- <td style="height: 30px;" colspan="3">
- </td>
- </tr>
- <tr>
- <td align="center" style="font-weight: bold; width: 98px; height: 30px;">
- <span style="color:Red">*</span><asp:Label ID="TitleLabel" runat="server"
- Text="Title"></asp:Label>
- </td>
- <td style="height: 30px" colspan="3" >
- <asp:TextBox ID="TitleTextBox" runat="server" Height="22px" Width="175px" MaxLength="50"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td align="center" style="font-weight: bold; width: 98px; height: 37px;">
- <asp:Label ID="DescriptionLabel" runat="server" Text="Description"></asp:Label>
- </td>
- <td style="height: 37px" colspan="3">
- <asp:TextBox ID="DescriptionTextBox" runat="server" Height="22px" Width="465px" MaxLength="250"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td align="center" style="font-weight: bold; width: 98px; height: 30px;">
- <span style="color:Red">*</span><asp:Label ID="pathLabel" runat="server"
- Text="path"></asp:Label> </td>
- <td style="height: 30px" colspan="3">
- <input ID="MyFile" runat="server" align="left" alt="rtl"
- onclick="return filMyFile_onclick()" type="file" visible="False" />
- <asp:TextBox ID="pathTextBox" runat="server" Height="22px" MaxLength="100"
- Width="467px" Visible="False"></asp:TextBox>
- <asp:FileUpload ID="FileUpload1" runat="server" />
- </td>
- </tr>
- <tr>
- <td align="center" style=" font-weight: bold; width: 98px; height: 36px;">
- <asp:Label ID="ReferenceLabel" runat="server"
- Text="Reference"></asp:Label>
- </td>
- <td style="height: 36px; width: 238px;" >
- <asp:TextBox ID="ReferenceTextBox" runat="server" Height="22px" MaxLength="100"
- Width="178px"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td align="center" style="font-weight: bold; width: 98px; height: 34px;">
- <span style="color:Red">*</span><asp:Label ID="TypeLabel" runat="server"
- Text="type"></asp:Label>
- </td>
- <td style="height: 34px; width: 238px;">
- <asp:DropDownList ID="TypeDropDownList" runat="server"
- DataSourceID="TypeSqlDataSource" DataTextField="name"
- DataValueField="id" Height="22px"
- ondatabound="TypeDropDownList_DataBound" Width="181px">
- </asp:DropDownList>
- </td>
- <td rowspan="4" align="center" style="width: 238px">
- <asp:Image ID="imgDB" runat="server" Visible="False" />
- </td>
- <td rowspan="4" style="width: 73px">
- </td>
- </tr>
- <tr>
- <td align="left" style="font-weight: bold; width: 98px; height: 34px;">
- <asp:Label ID="anniversaryLabel" runat="server" Text="anniversary"></asp:Label>
- </td>
- <td style="width: 238px; height: 50px;">
- <asp:TextBox ID="anniversaryTextBox" runat="server" Height="22px"
- MaxLength="15" Width="178px"></asp:TextBox>
- <asp:ImageButton ID="calendar_Date" runat="server"
- ImageUrl="~/images/Calendar.png" />
- <cc1:CalendarExtender ID="AddMyCalendar" runat="server" Enabled="True"
- FirstDayOfWeek="Saturday" Format="dd/MM/yyyy" PopupButtonID="calendar_Date"
- PopupPosition="BottomRight" TargetControlID="anniversaryTextBox">
- </cc1:CalendarExtender>
- </td>
- </tr>
- <tr>
- <td align="center" style="font-weight: bold; width: 98px; height: 34px;">
- </td>
- <td style="width: 238px; height: 50px;">
- </td>
- </tr>
- </table>
- </asp:Panel>
- </InsertItemTemplate>
- </asp:MyForm>
- <asp:ImageButton ID="AddImageButton" runat="server"
- ImageUrl="~/images/controls/upload 1.gif"
- Height="30px" Width="119px" onclick="AddImageButton_Click1" />
- <asp:SqlDataSource ID="TypeSqlDataSource" runat="server"
- ConnectionString="<%$ ConnectionStrings:info_con %>"
- ProviderName="<%$ ConnectionStrings:info_con.ProviderName %>"
- SelectCommand="SELECT * FROM picture_type" >
- </asp:SqlDataSource>
- <asp:SqlDataSource ID="picturesqlDataSource" runat="server"
- ConnectionString="<%$ ConnectionStrings:info_con %>"
- ProviderName="<%$ ConnectionStrings:info_con.ProviderName %>"
- InsertCommand="INSERT INTO pictures (title, description, type, path, reference, ac_id, anniversary) VALUES (@title, @description, @type, @path, @reference, 8, @anniversary)"
- SelectCommand="">
- <InsertParameters>
- <asp:Parameter Name="title" />
- <asp:Parameter Name="description" />
- <asp:Parameter Name="type" />
- <asp:Parameter Name="path" />
- <asp:Parameter Name="reference" />
- <asp:Parameter Name="anniversary" />
- </InsertParameters>
- <SelectParameters>
- <asp:Parameter Name="path" />
- </SelectParameters>
- </asp:SqlDataSource>
- </asp:Content>
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using MySql.Data.MySqlClient;
- using Webdll;
- using System.Collections.Generic;
- using System.IO;
- public partial class Add_Pictures : BasicPage
- {
- protected void Page_Init(object sender, EventArgs e)
- {
- base.SetMaster(Master, AddPictureMyForm);
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!this.IsPostBack)
- {
- Master.Labelb = "Adding";
- }
- }
- protected void Page_SaveStateComplete()
- {
- if ((AddPictureMyForm.Visible == true) && (AddPictureMyForm.FooterRow != null))
- {
- List<ControlsToValidate> list = new List<ControlsToValidate>();
- TextBox tx1 = (TextBox)AddPictureMyForm.FooterRow.FindControl("TitleTextBox");
- TextBox tx2 = (TextBox)AddPictureMyForm.FooterRow.FindControl("DescriptionTextBox");
- TextBox tx3 = (TextBox)AddPictureMyForm.FooterRow.FindControl("pathTextBox");
- TextBox tx4 = (TextBox)AddPictureMyForm.FooterRow.FindControl("ReferenceTextBox");
- TextBox tx5 = (TextBox)AddPictureMyForm.FooterRow.FindControl("anniversaryTextBox");
- ImageButton bt_add = AddImageButton;
- if (bt_add != null)
- {
- list.Add(new ControlsToValidate(tx1, CDataType.text, true, 50));
- list.Add(new ControlsToValidate(tx2, CDataType.text, false, 250));
- list.Add(new ControlsToValidate(tx3, CDataType.text, true, 100));
- list.Add(new ControlsToValidate(tx4, CDataType.text, false, 100));
- list.Add(new ControlsToValidate(tx5, CDataType.date, false, 15));
- ValidateControls(bt_add, list, message_type.alert);
- }
- }
- }
- private void WriteToFile(string FileName, ref byte[] Buffer)
- {
- FileStream fs = new FileStream(FileName, FileMode.Create);
- fs.Write(Buffer, 0, Buffer.Length);
- fs.Close();
- }
- private int WriteToDB(string fileName, ref byte[] Buffer)
- {
- picturesqlDataSource.SelectCommand = "SELECT id, COUNT(*) AS count FROM pictures WHERE path = " + fileName + " GROUP BY id;";
- DataView dv; DataRowView drv;
- dv = (DataView)picturesqlDataSource.Select(DataSourceSelectArguments.Empty);
- drv = dv[0];
- if (Convert.ToInt32(drv["count"]) > 0)
- { ScriptManager.RegisterStartupScript(this, this.GetType(), this.ToString(), @"<script language=""JavaScript"">window.alert('Duplicate in picture'); </script>", false); }
- else
- {
- string anniversary = "";
- try
- {
- DateTime dt = Convert.ToDateTime(((TextBox)AddPictureMyForm.FindControl("anniversaryTextBox")).Text);
- anniversary = dt.ToString("yyyy-MM-dd");
- }
- catch { }
- picturesqlDataSource.InsertParameters["title"].DefaultValue = ((TextBox)AddPictureMyForm.FindControl("TitleTextBox")).Text;
- picturesqlDataSource.InsertParameters["description"].DefaultValue = ((TextBox)AddPictureMyForm.FindControl("DescriptionTextBox")).Text;
- picturesqlDataSource.InsertParameters["path"].DefaultValue = fileName;
- picturesqlDataSource.InsertParameters["reference"].DefaultValue = ((TextBox)AddPictureMyForm.FindControl("ReferenceTextBox")).Text;
- picturesqlDataSource.InsertParameters["type"].DefaultValue = ((DropDownList)AddPictureMyForm.FindControl("TypeDropDownList")).SelectedValue;
- picturesqlDataSource.InsertParameters["anniversary"].DefaultValue = anniversary;
- //picturesqlDataSource.InsertParameters["Data"].DefaultValue = Buffer.ToString();
- try // test inesrt command
- {
- picturesqlDataSource.Insert();
- Action_status = event_status.success;
- ScriptManager.RegisterStartupScript(this, this.GetType(), this.ToString(), @"<script language=""JavaScript"">window.alert('Successfully Done'); </script>", false);
- picturesqlDataSource.SelectCommand = "SELECT id, COUNT(*) AS count FROM pictures WHERE id = (SELECT MAX(id) FROM pictures) GROUP BY id";
- dv = (DataView)picturesqlDataSource.Select(DataSourceSelectArguments.Empty);
- drv = dv[0];
- }
- catch
- { ScriptManager.RegisterStartupScript(this, this.GetType(), this.ToString(), @"<script language=""JavaScript"">window.alert('Error in inserting '); </script>", false); }
- }
- //Get Id
- int id = Convert.ToInt32(drv["id"]);
- return id;
- }
- protected void TypeDropDownList_DataBound(object sender, EventArgs e)
- {
- ListItem li = new ListItem("select a type", "0");
- ((DropDownList)sender).Items.Insert(0, li);
- }
- 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 AddImageButton_Click1(object sender, ImageClickEventArgs e)
- {
- Action_status = event_status.fail;
- FileUpload PicUpload = (FileUpload)AddPictureMyForm.FindControl("FileUpload1");
- if (((DropDownList)AddPictureMyForm.FindControl("TypeDropDownList")).SelectedIndex == 0)
- { ScriptManager.RegisterStartupScript(this, this.GetType(), this.ToString(), @"<script language=""JavaScript"">window.alert('must choose a type'); </script>", false); return; }
- if (PicUpload.PostedFile == null) { ScriptManager.RegisterStartupScript(this, this.GetType(), this.ToString(), @"<script language=""JavaScript"">window.alert('Error'); </script>", false); return; }
- HttpPostedFile myFile = PicUpload.PostedFile; // Get a reference to PostedFile object
- int myFileLen = myFile.ContentLength; // Get size of uploaded file
- if (myFileLen > 0)
- {
- byte[] myData = new byte[myFileLen]; // buffer to read
- myFile.InputStream.Read(myData, 0, myFileLen); //read uploaded file from stream
- string Filename = Path.GetFileName(myFile.FileName);
- WriteToFile(Server.MapPath(Filename), ref myData);
- int FileID = WriteToDB(Filename, ref myData);
- ((Image)AddPictureMyForm.FindControl("imgDB")).ImageUrl = GetMyName() + "?FileID=" + FileID.ToString();
- ((Image)AddPictureMyForm.FindControl("imgDB")).Visible = true;
- }
- }
- protected void filMyFile_onclick(object sender, ImageClickEventArgs e)
- {
- HtmlInputFile PicUpload = (HtmlInputFile)AddPictureMyForm.FindControl("MyFile");
- if (PicUpload.PostedFile != null)
- {
- }
- else
- { }
- }
- }
thanks :)