Hello, I am peggie. Currently, I am using asp.net visual c#. I have a zip file web gallery that I had download from this site. But, the problem is the upload button is in visual basic code while I need one is visual c # code. Can u guys help me to convert to visual c# code? Thanks in advance:)
Imports
System.ioImports
System.DataImports
System.Data.SqlClientPartial
Class UploadPhoto Inherits System.Web.UI.Page Protected Sub btnAddToCategory_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddToCategory.Click Try Dim strGuid As String = "" Dim strPicExtension As String = "" Dim strFileName As String = "" ' Firstly upload the picture associated with the news If PhotoUpload.HasFile Then ' Constructs the picture extensionstrGuid = fnGuid()
strPicExtension = fnGetPictureExtension(PhotoUpload.FileName)
strFileName = strGuid & strPicExtension
' Add picture to foldersubAddNewPicture(PhotoUpload, strFileName,
"Gallery") ' Add the record to the databaseUpdateGallery(cmbCategory.SelectedValue, strFileName, txtMemo.Text.Trim())
lblUploadMessage.Text =
"Image uploaded successfully..." ElselblUploadMessage.Text =
"Please select appropriate image..." End IflblUploadMessage.Visible =
True Catch ex As Exception Throw ex End Try End Sub#
Region "DataLayer" Private Sub UpdateGallery(ByVal strCategoryValue As String, ByVal strFileName As String, ByVal strMemo As String) ' Variables declaration Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString() Dim sqlConn As New SqlConnection(strConnString) Dim sqlcmd As New SqlCommand() ' Building the query Dim sqlQuery_Photo As String = "INSERT INTO [Photo] VALUES ('{0}', '{1}', '{2}')"sqlQuery_Photo = sqlQuery_Photo.Replace(
"{0}", strCategoryValue)sqlQuery_Photo = sqlQuery_Photo.Replace(
"{1}", strFileName)sqlQuery_Photo = sqlQuery_Photo.Replace(
"{2}", strMemo) ' Retrieving search resultsqlConn.Open()
sqlcmd.Connection = sqlConn
' Adding records to tablesqlcmd.CommandText = sqlQuery_Photo
sqlcmd.ExecuteNonQuery()
sqlConn.Close()
End Sub#
End Region#
Region "Private Functions" ' Adds picture to specified folder Private Sub subAddNewPicture(ByVal FileUploader As FileUpload, ByVal strPictureName As String, ByVal strImgFolder As String) Dim strImageFolderPath As String Dim strImagePath As String Try ' Construct saving pathstrImageFolderPath = Path.Combine(Request.PhysicalApplicationPath, strImgFolder)
strImagePath = Path.Combine(strImageFolderPath, strPictureName)
' Upload imageFileUploader.SaveAs(strImagePath)
Catch ex As Exception Throw ex End Try End Sub ' Function to generate a GUID using current date and time Private Function fnGuid() As String Dim strGuid As String = "" Try With TodaystrGuid = .Year & .Month & .Day & Now.Hour & Now.Minute & Now.Second
End With Catch ex As Exception Throw ex End Try ' returning the guid generated Return strGuid End Function ' Returns true if file name has the extension .jpg, .gif, .jpeg Private Function fnGetPictureExtension(ByVal strPictureName As String) As String Try With strPictureName.ToUpper If .EndsWith(".JPG") Then Return ".JPG" ElseIf .EndsWith(".GIF") Then Return ".GIF" ElseIf .EndsWith(".JPEG") Then Return ".JPEG" End If End With Catch ex As Exception Throw ex End Try ' Else if has no extension so it returns "" Return "" End Function#
End RegionEnd
Class
Bechir BejaouiPosted Dec 29, 2008, 5:42 PM
Riaan de LangePosted Dec 27, 2008, 5:28 AM
Link: http://forums.asp.net/t/1314330.aspx
Regards,
Bechir BejaouiPosted Dec 22, 2008, 5:10 AM
Is there any bugs with the code?
Peggie LohPosted Dec 21, 2008, 8:21 PM
Bechir BejaouiPosted Dec 21, 2008, 8:05 PM
using System.Data;
using System.Data.SqlClient;
partial class UploadPhoto : System.Web.UI.Page{ protected void // ERROR: Handles clauses are not supported in C# btnAddToCategory_Click(object sender, System.EventArgs e) { try { string strGuid = ""; string strPicExtension = ""; string strFileName = ""; // Firstly upload the picture associated with the news if (PhotoUpload.HasFile) { // Constructs the picture extension strGuid = fnGuid(); strPicExtension = fnGetPictureExtension(PhotoUpload.FileName); strFileName = strGuid + strPicExtension; // Add picture to folder subAddNewPicture(PhotoUpload, strFileName, "Gallery"); // Add the record to the database UpdateGallery(cmbCategory.SelectedValue, strFileName, txtMemo.Text.Trim()); lblUploadMessage.Text = "Image uploaded successfully..."; } else { lblUploadMessage.Text = "Please select appropriate image..."; } lblUploadMessage.Visible = true; } catch (Exception ex) { throw ex; } } #region "DataLayer" private void UpdateGallery(string strCategoryValue, string strFileName, string strMemo) { // Variables declaration string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString(); SqlConnection sqlConn = new SqlConnection(strConnString); SqlCommand sqlcmd = new SqlCommand(); // Building the query string sqlQuery_Photo = "INSERT INTO [Photo] VALUES ('{0}', '{1}', '{2}')"; sqlQuery_Photo = sqlQuery_Photo.Replace("{0}", strCategoryValue); sqlQuery_Photo = sqlQuery_Photo.Replace("{1}", strFileName); sqlQuery_Photo = sqlQuery_Photo.Replace("{2}", strMemo); // Retrieving search result sqlConn.Open(); sqlcmd.Connection = sqlConn; // Adding records to table sqlcmd.CommandText = sqlQuery_Photo; sqlcmd.ExecuteNonQuery(); sqlConn.Close(); } #endregion #region "Private Functions" // Adds picture to specified folder private void subAddNewPicture(FileUpload FileUploader, string strPictureName, string strImgFolder) { string strImageFolderPath = null; string strImagePath = null; try { // Construct saving path strImageFolderPath = Path.Combine(Request.PhysicalApplicationPath, strImgFolder); strImagePath = Path.Combine(strImageFolderPath, strPictureName); // Upload image FileUploader.SaveAs(strImagePath); } catch (Exception ex) { throw ex; } } // Function to generate a GUID using current date and time private string fnGuid() { string strGuid = ""; try { { strGuid = Today.Year + Today.Month + Today.Day + Now.Hour + Now.Minute + Now.Second; } } catch (Exception ex) { throw ex; } // returning the guid generated return strGuid; } // Returns true if file name has the extension .jpg, .gif, .jpeg private string fnGetPictureExtension(string strPictureName) { try { { if (strPictureName.ToUpper.EndsWith(".JPG")) { return ".JPG"; } else if (strPictureName.ToUpper.EndsWith(".GIF"))
{ return ".GIF"; }
else if (strPictureName.ToUpper.EndsWith(".JPEG"))
{ return ".JPEG"; } }
} catch (Exception ex) {throw ex; } // Else if has no extension so it returns ""
return "";
}
}
#endregion