Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello


I want to save information of a person with his photo in sql database using asp.net how to do it.



Regards


Amy
Posted
Comments
[no name] 20-Jul-13 8:22am    
Connect to your database, write a query to insert your user information to the database, execute the query, disconnect from the database.

Provide an upload control, on the user details page, and let him upload an image file.
Then use a parameterized query to insert the data: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] shows you how, but you don't need to convert the image to a byte array first as it will arrive as a byte array with the file upload control anyway.
 
Share this answer
 
add a file upload control , and try this here is a sample i'm providing
C#
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["SampleConnectionString"].ConnectionString);
	
public void AddProfile(string id, ,byte[] pic)
    {

        con.Open();
        SqlCommand cmdIns = new SqlCommand("INSERT INTO profile (id,img) VALUES (@id,@pic)", con);
        cmdIns.Parameters.Add("@id", SqlDbType.VarChar).Value = id;
        cmdIns.Parameters.Add("@pic", SqlDbType.Image).Value = pic;
        cmdIns.ExecuteNonQuery();
        con.Close();
    }

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int l=uploadPhoto.PostedFile.ContentLength; 
        byte[] pic= new byte[l];
        uploadPhoto.PostedFile.InputStream.Read(pic,0,l);
        string typ = uploadPhoto.PostedFile.ContentType;
        if ((typ == "image/jpeg" || typ == "image/jpg" || typ == "image/gif" || typ == "image/png" || typ == "image/bmp") && (l < 51200))
        {
            AddProfile(UserID,pic)
                lblMessage.Text = "Profile Added !!";
           
        }
        else
        {
            lblMessage.Text = "! Image size must be less than 50kb and type must be (.jpeg/.jpg/.png/.gif/.bmp)";
        }
    }
 
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