i am building a social network site using asp.net c# , And i am having a problem with image uploading, when i upload an image to the database it name will be changed and it save on real path i mentioned. but i cannot get it to the database table.
This is code
<asp:FileUpload ID="ProfilePic" runat="server" style="margin-top: -10px" />
<asp:Button ID="Register" runat="server" Text=" Register"
onclick="Button2_Click" ValidationGroup="RegisterGroup" Font-Size="Larger" Height="30px" style="text-align: left; margin-left: 106px" Width="103px" />
This is code behind file
if (ProfilePic.HasFile)
{
if ((ProfilePic.PostedFile.ContentType == "image/jpeg") ||
(ProfilePic.PostedFile.ContentType == "image/png") ||
(ProfilePic.PostedFile.ContentType == "image/bmp") ||
(ProfilePic.PostedFile.ContentType == "image/gif"))
{
if (Convert.ToInt64(ProfilePic.PostedFile.ContentLength) < 10000000)
{
string photofolder = Path.Combine(@"C:\Users\Supun\Documents\Visual Studio 2013\WebSites\MeetYou\ProfilePic", User.Identity.Name);
if (!Directory.Exists(photofolder))
Directory.CreateDirectory(photofolder);
string extension = Path.GetExtension(ProfilePic.FileName);
string uniqueFileName = Path.ChangeExtension(ProfilePic.FileName, DateTime.Now.Ticks.ToString());
ProfilePic.SaveAs(Path.Combine(photofolder, uniqueFileName + extension));
// string harshpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(RegPassword.Text, "shar1");
if (IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string checkemail = "select count(*) from UserData where Email='" + RegEmail.Text + "'";
SqlCommand com = new SqlCommand(checkemail, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
string message = "Email already Exits";
ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + "');", true);
}
else{
try{
// SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
Guid newGuid = Guid.NewGuid();
string inserQuery = "insert into UserData(YourName,Email,Password,Gender,Birthday,AboutMe,Country,ID) values (@YName,@REmail,@RPassword,@DDownGender,@MTextBox,@RAboutMe,@DCountry,@ID)";
SqlCommand comm = new SqlCommand(inserQuery, conn);
comm.Parameters.AddWithValue("@YName", Your_Name.Text);
comm.Parameters.AddWithValue("@REmail", RegEmail.Text);
comm.Parameters.AddWithValue("@RPassword", RegPassword.Text);
comm.Parameters.AddWithValue("@DDownGender", DropDownGender.SelectedItem.ToString());
comm.Parameters.AddWithValue("@MTextBox", Birthday.Text);
comm.Parameters.AddWithValue("@RAboutMe", RegAboutMe.Text);
comm.Parameters.AddWithValue("@DCountry", DropDownCountry.SelectedItem.ToString());
comm.Parameters.AddWithValue("@ID", newGuid.ToString());
comm.Parameters.AddWithValue("@ProfilePic", uniqueFileName);
comm.ExecuteNonQuery();
Session["RegEmail"] = RegEmail.Text;
Response.Redirect("SecurityQuestion.aspx");
// string message = "Registration Complete!!! Please login in";
// ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + "');", true);
}
catch (Exception ex) {
Response.Write("Error -->" + ex.ToString());
}
}
conn.Close();
}
And this is sql code
CREATE TABLE [dbo].[UserData] (
[YourName] VARCHAR (50) NULL,
[Email] VARCHAR (50) NOT NULL,
[Password] VARCHAR (50) NULL,
[Gender] VARCHAR (50) NULL,
[Birthday] VARCHAR (50) NULL,
[AboutMe] VARCHAR (50) NULL,
[Country] VARCHAR (50) NULL,
[ID] NVARCHAR (50) NOT NULL,
[ProfilePic ] IMAGE NULL,
CONSTRAINT [PK_UserData] PRIMARY KEY CLUSTERED ([Email] ASC)
);
Could someboday tell me how to get this name changing image to the database table, And tell me guys what is the good method to save images on database table,
now i use this to save image
[ProfilePic ] IMAGE NULL,