Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a new user sign-up page and also a log-in page. In sign-up page user can also upload their picture.
Please help me modify the log-in code in a way that when user logs in then only image upload by that user will be displayed

login.cs

protected void Button1_Click(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\...\Database.mdf;Integrated Security=True;User Instance=True");
       con.Open();
       string query = "Select Password from Table1 Where Email='" + TextBox2.Text + "'";
       SqlCommand com = new SqlCommand(query, con);
       SqlDataReader rd;
       rd = com.ExecuteReader();
       while (rd.Read())
       {
           s = rd[0].ToString();
       }
       con.Close();
       if (s == TextBox1.Text)
       {
           Session["Email"] = TextBox2.Text;
           Response.Redirect("information.aspx");
       }
       else
       {
           //Response.Redirect("home.aspx");
           Response.Write("<script language='javascript'>alert( 'Invalid password or Email ' )</script>");

       }

Signup.cs
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con1 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\...\Database.mdf;Integrated Security=True;User Instance=True");
        con1.Open();
        SqlCommand sc = new SqlCommand("Select Email from Table1", con1);
        SqlDataReader rd;
        rd = sc.ExecuteReader();
        while (rd.Read())
        {
            // Label25.Text = rd["Stid"].ToString();
            if (rd["Email"].ToString().Equals(TextBoxEmail.Text))
            {
                Response.Write("<script language='javascript'>alert( 'Already Exsist ' )</script>");

                k = 1;

            }
        }
        con1.Close();
        if (k == 0)
        {
            string fileName = FileUpload1.PostedFile.FileName;
            int fileLength = FileUpload1.PostedFile.ContentLength;
            byte[] imageBytes = new byte[fileLength];
            FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, fileLength);
            string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString();
            using (SqlConnection conn = new SqlConnection(connStr))
            {

                string query1 = "insert into Table1(Email,Password,confirmPassword,Name,Country,Gender,Year,Date,Month,Description,PictureFile)values('" + TextBoxEmail.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBoxName.Text + "','" + TextBox1.Text + "','" + DropDownList1.Text + "','" + DropDownList4.Text + "','" + DropDownList2.Text + "','" + DropDownList3.Text + "','" + TextBox2.Text + "',@pictureFile)";
                SqlParameter[] prms = new SqlParameter[1];
                prms[0] = new SqlParameter("@pictureFile", SqlDbType.Image);
                prms[0].Value = imageBytes;
                using (SqlCommand cmd = new SqlCommand(query1, conn))
                {
                    cmd.Parameters.AddRange(prms);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }

                Response.Redirect("Login.aspx");

            }
Posted
Updated 17-Mar-11 20:44pm
v3
Comments
Ankur\m/ 18-Mar-11 2:48am    
I don't see a problem with the code (except for the obvious SQL-injection security loop hole). In the Information.aspx get the user specific data and show the details including the image.
Where is the problem?

1 solution

You need to embed an image into your webpage:
<img alt="" src="ImageFromDb.ashx" />

You then need to code to access and display the image.
In VS, right click your project, and select "Add new Item"
When the dialog appears, select "Generic Handler" from the list, and call it "InamgeFromDb"
Press [ENTER].
The code below will display an image dynamically:
<%@ WebHandler Language="C#" Class="ImageFromDb" %>

C#
using System;
using System.Web;
using System.Data.SqlClient;
public class ImageFromDb : IHttpHandler
    {
    public bool IsReusable
        {
        get { return false; }
        }
    public void ProcessRequest(HttpContext context)
        {
        using (SqlConnection con = new SqlConnection(@"Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=myDatabase;Integrated Security=True"))
            {
            con.Open();
            //SqlCommand com = new SqlCommand("SELECT * FROM myTable", con);
            SqlCommand com = new SqlCommand("SELECT * FROM myTable WHERE id=1 AND Username='Hello'", con);
            com.Parameters.AddWithValue("@DT", DateTime.Now);
            SqlDataReader read = com.ExecuteReader();
            while (read.Read())
                {
                object o = read["Image"];
                if (!(o is DBNull))
                    {
                    // Image found.
                    context.Response.ContentType = "image/JPEG";
                    context.Response.BinaryWrite((byte[]) o);
                    }
                }
            }
        }
    }
You will have to modify this code to suit your database and user id system.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900