locked
Datalist Bind Issue RRS feed

  • Question

  • User-521826128 posted
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <table>
        <tr>
        <td colspan="3">
        <h2>ImageGallery</h2>
        </td>
        </tr>
        <tr>
        <td>
        Upload Image
        </td>
        <td>
        <asp:FileUpload ID="ImgFileUpload" runat="server" />
        </td>
        <td>
        <asp:Button ID="btnupload" runat="server" Text="Upload" onclick="btnupload_Click" />
        </td>
        </tr>
        </table>
        </div>
        <div id="divImageList" runat="server" style="width:120%; margin-left:10%">
        <asp:DataList ID="dtListImageGallery" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="50%" BorderColor="Black" BorderStyle="Double" BorderWidth="2px">
        <ItemTemplate>
        <br />
        <asp:Image Width="200px" ID="ImageGallery" ImageUrl='<%#Bind("ImagePath","~/ImageGallery/{0}")%>' runat="server" />
        </ItemTemplate>
       
        <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
        </asp:DataList>
        </div>
        </form>
        </body>
        </html>
    DataList.aspx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.IO;
    using System.Data;
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataList();
            }
        }
        protected void btnupload_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source=ANSARI-PC\\SQLEXPRESS; initial catalog=Shuklaji; integrated security=true");
            if (ImgFileUpload.PostedFile!=null)
            {
                string imgname=Path.GetFileName(ImgFileUpload.PostedFile.FileName);
                imgname=imgname.Split('.') [0].ToString();
                string pathname=Server.MapPath("~\\ImageGallery\\")+ DateTime.Now.ToString("ddmmyyhhmmsstt")+ImgFileUpload.FileName;
                ImgFileUpload.PostedFile.SaveAs(pathname);
                try
                {
                    con.Open();
                    string insquery = "Insert into tblimg(ImageName,ImagePath)values ('" + imgname + "','" + DateTime.Now.ToString("ddmmyyhhmmsstt") + ImgFileUpload.FileName.ToString() + "')";
                    SqlCommand cmd=new SqlCommand(insquery,con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    BindDataList();
                }
                catch(Exception ex)
                {
                    Response.Write(ex.Message);
                }
                Response.Redirect("Default.aspx");
            }
        }
        private void BindDataList()
        {
            SqlConnection con = new SqlConnection(@"Data Source=ANSARI-PC\SQLEXPRESS; initial catalog=Shuklaji; integrated security=true");
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from tblimg", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            da.Dispose();
            dtListImageGallery.DataSource = dt;
            dtListImageGallery.DataBind();
            con.Close();
        }
    }
    The table image has 3 fields, ImageId, ImageName, ImagePath and the images are getting saved in the image gallery folder. Now the main issue is that the pics are not getting saved in the db and are not binding in the datalist also. Now My question is , what is the error in the following code....?  Please don't post any new code. Make changes in my code only...Thanks in advance
    Wednesday, November 30, 2016 1:48 PM

Answers

  • User-707554951 posted

    Hi Shuklaji123,

    Based on your needs, I make an example according to your code.

    You could refer to it:

      <div>
        <table>
        <tr>
        <td colspan="3">
        <h2>ImageGallery</h2>
        </td>
        </tr>
        <tr>
        <td>
        Upload Image
        </td>
        <td>
        <asp:FileUpload ID="ImgFileUpload" runat="server" />
        </td>
        <td>
        <asp:Button ID="btnupload" runat="server" Text="Upload" onclick="btnupload_Click" />
        </td>
        </tr>
        </table>
        </div>
        <div id="divImageList" runat="server" style="width:120%; margin-left:10%">
        <asp:DataList ID="dtListImageGallery" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="50%" BorderColor="Black" BorderStyle="Double" BorderWidth="2px">
        <ItemTemplate>
        <br />
        <asp:Image Width="200px" ID="ImageGallery" ImageUrl='<%#Bind("ImagePath")%>' runat="server" />
        </ItemTemplate>
       
        <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
        </asp:DataList>
        </div>

    CodeBehind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindDataList();
                }
            }
            protected void btnupload_Click(object sender, EventArgs e)
            {
               
                if (ImgFileUpload.PostedFile != null)
                {
    
                    string imgname = ImgFileUpload.FileName.ToString();
                    ImgFileUpload.PostedFile.SaveAs(Server.MapPath("~/ImageGallery/") + imgname);
                    string pathsve = "~/ImageGallery/" + imgname;
                        string strConnString = System.Configuration.ConfigurationManager.
               ConnectionStrings["ConnectionString"].ConnectionString;
                        SqlConnection con = new SqlConnection(strConnString);
                        con.Open();
                        string insquery = "Insert into tblimg(ImageName,ImagePath)values ('" + imgname + "','"+ pathsve + "')";
                        SqlCommand cmd = new SqlCommand(insquery, con);
                        cmd.ExecuteNonQuery();
                        con.Close();
                        BindDataList();
                    }                       
            }
    
            private void BindDataList()
            {
                string strConnString = System.Configuration.ConfigurationManager.
               ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("select * from tblimg", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                da.Dispose();
                dtListImageGallery.DataSource = dt;
                dtListImageGallery.DataBind();
                con.Close();
            }

    Output screenshot as below:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 14, 2016 3:33 AM

All replies

  • User-707554951 posted

    Hi Shuklaji123,

    For your code you provide us are in an mess, So, it is difficult for me to find your problem,

    Would you please provide me with new ordered code?

    Click the point I highlighted, paste your code in there:

    Best regards

    Cathy

    Thursday, December 1, 2016 6:21 AM
  • User-521826128 posted

    Thanks Cathy, will surely do that....

    Thursday, December 1, 2016 1:18 PM
  • User-521826128 posted

    hi everyone, here is the datalist code

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <table>
        <tr>
        <td colspan="3">
        <h2>ImageGallery</h2>
        </td>
        </tr>
        <tr>
        <td>
        Upload Image
        </td>
        <td>
        <asp:FileUpload ID="ImgFileUpload" runat="server" />
        </td>
        <td>
        <asp:Button ID="btnupload" runat="server" Text="Upload" onclick="btnupload_Click" />
        </td>
        </tr>
        </table>
        </div>
        <div id="divImageList" runat="server" style="width:120%; margin-left:10%">
        <asp:DataList ID="dtListImageGallery" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="50%" BorderColor="Black" BorderStyle="Double" BorderWidth="2px">
        <ItemTemplate>
        <br />
        <asp:Image Width="200px" ID="ImageGallery" ImageUrl='<%#Bind("ImagePath","~/ImageGallery/{0}")%>' runat="server" />
        </ItemTemplate>
       
        <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
        </asp:DataList>
        </div>
        </form>
        </body>
        </html>
    
    
    

    Friday, December 2, 2016 3:43 PM
  • User-521826128 posted
    DataList.aspx.cs
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.IO;
    using System.Data;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataList();
            }
    
        }
        protected void btnupload_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source=ANSARI-PC\\SQLEXPRESS; initial catalog=Shuklaji; integrated security=true");
            if (ImgFileUpload.PostedFile!=null)
            {
                string imgname=Path.GetFileName(ImgFileUpload.PostedFile.FileName);
                imgname=imgname.Split('.') [0].ToString();
                string pathname=Server.MapPath("~\\ImageGallery\\")+ DateTime.Now.ToString("ddmmyyhhmmsstt")+ImgFileUpload.FileName;
                ImgFileUpload.PostedFile.SaveAs(pathname);
                try
                {
                    con.Open();
                    string insquery = "Insert into tblimg(ImageName,ImagePath)values ('" + imgname + "','" + DateTime.Now.ToString("ddmmyyhhmmsstt") + ImgFileUpload.FileName.ToString() + "')";
                    SqlCommand cmd=new SqlCommand(insquery,con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    BindDataList();
                }
                catch(Exception ex)
                {
                    Response.Write(ex.Message);
                }
                Response.Redirect("Default.aspx");
            }
        }
        private void BindDataList()
        {
            SqlConnection con = new SqlConnection(@"Data Source=ANSARI-PC\SQLEXPRESS; initial catalog=Shuklaji; integrated security=true");
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from tblimg", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            da.Dispose();
            dtListImageGallery.DataSource = dt;
            dtListImageGallery.DataBind();
            con.Close();
        }
    }
    
    the image is not inserting in database and is not binding too. Please help...
    
    
    
    
    
    

    Friday, December 2, 2016 3:45 PM
  • User-1479952219 posted

    I think Problem in database connection. Change in code and check.
    SqlConnection con = new SqlConnection("Data Source=ANSARI-PC\SQLEXPRESS; Initial Catalog=Shuklaji; Integrated Security=true");

    Monday, December 5, 2016 1:28 PM
  • User-521826128 posted

    Are you sure ? Please check the code and let me know. I cannot find the error. Thanks The connection string is working fine. Still it is null. Please help me find error. Thanks 

    Please check this much code and let me know, why is it not working ? 

      protected void btnupload_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source=ANSARI-PC\\SQLEXPRESS;initial catalog=Shuklaji;integrated security=true");
            if (ImgFileUpload.PostedFile!=null)
            {
                string imgname=Path.GetFileName(ImgFileUpload.PostedFile.FileName);
                imgname=imgname.Split('.') [0].ToString();
                string pathname=Server.MapPath("~\\ImageGallery\\")+ DateTime.Now.ToString("ddmmyyhhmmsstt")+ImgFileUpload.FileName;
                ImgFileUpload.PostedFile.SaveAs(pathname);
                try
                {
                    con.Open();
                    string insquery = "Insert into tblimg(ImageName,ImagePath)values ('" + imgname + "','" + DateTime.Now.ToString("ddmmyyhhmmsstt") + ImgFileUpload.FileName.ToString() + "')";
                    SqlCommand cmd=new SqlCommand(insquery,con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    BindDataList();
                }
                catch(Exception ex)
                {
                    Response.Write(ex.Message);
                }
                Response.Redirect("Default.aspx");
            }
        }
      

    Tuesday, December 6, 2016 2:16 AM
  • User-521826128 posted

    I  have not got any help in this case. Please let me know. Looking for prompt reply. Thanks 

    Tuesday, December 13, 2016 10:25 AM
  • User-707554951 posted

    Hi Shuklaji123,

    Based on your needs, I make an example according to your code.

    You could refer to it:

      <div>
        <table>
        <tr>
        <td colspan="3">
        <h2>ImageGallery</h2>
        </td>
        </tr>
        <tr>
        <td>
        Upload Image
        </td>
        <td>
        <asp:FileUpload ID="ImgFileUpload" runat="server" />
        </td>
        <td>
        <asp:Button ID="btnupload" runat="server" Text="Upload" onclick="btnupload_Click" />
        </td>
        </tr>
        </table>
        </div>
        <div id="divImageList" runat="server" style="width:120%; margin-left:10%">
        <asp:DataList ID="dtListImageGallery" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="50%" BorderColor="Black" BorderStyle="Double" BorderWidth="2px">
        <ItemTemplate>
        <br />
        <asp:Image Width="200px" ID="ImageGallery" ImageUrl='<%#Bind("ImagePath")%>' runat="server" />
        </ItemTemplate>
       
        <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
        </asp:DataList>
        </div>

    CodeBehind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindDataList();
                }
            }
            protected void btnupload_Click(object sender, EventArgs e)
            {
               
                if (ImgFileUpload.PostedFile != null)
                {
    
                    string imgname = ImgFileUpload.FileName.ToString();
                    ImgFileUpload.PostedFile.SaveAs(Server.MapPath("~/ImageGallery/") + imgname);
                    string pathsve = "~/ImageGallery/" + imgname;
                        string strConnString = System.Configuration.ConfigurationManager.
               ConnectionStrings["ConnectionString"].ConnectionString;
                        SqlConnection con = new SqlConnection(strConnString);
                        con.Open();
                        string insquery = "Insert into tblimg(ImageName,ImagePath)values ('" + imgname + "','"+ pathsve + "')";
                        SqlCommand cmd = new SqlCommand(insquery, con);
                        cmd.ExecuteNonQuery();
                        con.Close();
                        BindDataList();
                    }                       
            }
    
            private void BindDataList()
            {
                string strConnString = System.Configuration.ConfigurationManager.
               ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("select * from tblimg", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                da.Dispose();
                dtListImageGallery.DataSource = dt;
                dtListImageGallery.DataBind();
                con.Close();
            }

    Output screenshot as below:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 14, 2016 3:33 AM
  • User-521826128 posted

    Thanks a lot , will try your code and let me know.. Thanks a lot !!!!!!!!!!!!!!!!

    Wednesday, December 14, 2016 9:50 AM
  • User-521826128 posted

    Thanks Cathy,I am posing another question of delete, that if I want to delete an image from the datalist , how to do that , everything being same, thanks a lot in advance... I am posting the image of a page, please let me know where is the problem ? open it .. The images are not seen in the webpage.. 

    Thursday, December 22, 2016 7:48 AM