Click here to Skip to main content
15,041,025 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
MY TABLE IS
UserId
ImageName varchar(500)

pictures is the folder

i also set ImageField=ImageName

mycode is

 string sImageFileExtension;
    SqlConnection conn=new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2005\WebSites\Project2\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
    DataBaseClass dbClass = new DataBaseClass();
    public DataTable dt;
    string connstr=@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2005\WebSites\Project2\App_Data\Database.mdf;Integrated Security=True;User Instance=True";
    protected void Page_Load(object sender, EventArgs e)
    {
   
        Load_GridData();
    }


void Load_GridData()
    {
        conn.Open();
        SqlDataAdapter Sqa = new SqlDataAdapter("select * from Pictures Where UserId=('"+Session["Id"].ToString()+"')", conn);
        DataSet ds = new DataSet();
        Sqa.Fill(ds);   // fill the dataset 
        GridView1.DataSource = ds; // give data to GridView
        GridView1.DataBind();
        conn.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile != null)
        {
            string myMap = MapPath("~/");
            Random r = new Random();
            int next = r.Next();
            string ImageName = FileUpload1.PostedFile.FileName;
            sImageFileExtension = ImageName.Substring(ImageName.LastIndexOf(".")).ToLower();
            if (sImageFileExtension == ".gif" || sImageFileExtension == ".png" || sImageFileExtension == ".jpg" || sImageFileExtension == ".jpeg" || sImageFileExtension == ".bmp")
            {
                string ImageSaveURL = myMap + "Pictures/" +next+ sImageFileExtension;
                try
                {
                    FileUpload1.PostedFile.SaveAs(ImageSaveURL);
                    //  Session["Id"]=dt.Rows[0]["Id"].ToString();
                    string RegisterQuery = "insert into Pictures(UserId,ImageName)values('" + Session["Id"].ToString()+ "','"+next+sImageFileExtension+"')";
                    dbClass.ConnectDataBaseToInsert(RegisterQuery);
                    Response.Redirect("~/Default3.aspx");
                }
    catch (Exception ex)
                {
                }
            }
            else
            {
            }
        }
    }
}


Image is not displayed in gridview in databound field cross is displayed
Posted
Updated 13-May-11 8:16am
v2

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