/
ComingSoonAdmin.aspx.cs
149 lines (124 loc) · 4.52 KB
/
ComingSoonAdmin.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ComingSoon2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string GetImage(object img)
{
return "data:image/jpg;base64," + Convert.ToBase64String((byte[])img);
}
protected void btnInsert_Click(object sender, EventArgs e)
{
// Read the file and convert it to Byte Array
string filePath = fpPoster.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
//Set the content type based on File Extension
switch (ext)
{
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
}
if (contenttype != String.Empty)
{
Stream fs = fpPoster.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//insert the file into database
string strQuery = "INSERT into ComingSoon(Name, Poster, Trailer_url, Description, Director, Length)" +
" values ( @Name, @Poster, @Trailer_url, @Description, @Director, @Length)";
SqlCommand cmd = new SqlCommand(strQuery);
string name = txtName.Text;
cmd.Parameters.AddWithValue("Name", name);
cmd.Parameters.Add("@Poster", SqlDbType.Binary).Value = bytes;
string trailerURL = txtTrailerURL.Text;
cmd.Parameters.AddWithValue("Trailer_url", trailerURL);
string description = txtDescription.Text;
cmd.Parameters.AddWithValue("Description", description);
string director = txtDirector.Text;
cmd.Parameters.AddWithValue("Director", director);
string length = txtLength.Text;
cmd.Parameters.AddWithValue("Length", length);
InsertUpdateData(cmd);
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "File Uploaded Successfully";
txtName.Text = "";
txtTrailerURL.Text = "";
txtDescription.Text = "";
txtLength.Text = "";
txtDirector.Text = "";
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "File format not recognised." +
" Upload Image formats";
}
}
private Boolean InsertUpdateData(SqlCommand cmd)
{
//creates connection for the data source and executes the query.
String connectionString = ConfigurationManager.ConnectionStrings["MovieDBConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
finally
{
gvwCSAdmin.DataBind();
con.Close();
con.Dispose();
}
}
//error handling for row deleted
protected void gvwCSAdmin_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
if (e.Exception != null)
{
lblErrorMessage.Text = "A database error has occurred.<br /><br />" +
"Message: " + e.Exception.Message;
e.ExceptionHandled = true;
}
else if (e.AffectedRows == 0)
lblErrorMessage.Text = "Another user may have updated that category." +
"<br />Please try again.";
}
//error handling for row updated
protected void gvwCSAdmin_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
if (e.Exception != null)
{
lblMessage.Text = "A database error has occurred.<br /><br />" +
"Message: " + e.Exception.Message;
e.ExceptionHandled = true;
}
else if (e.AffectedRows == 0)
lblMessage.Text = "Another user may have updated that category." +
"<br />Please try again.";
}
}