Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to upload a image into database



Please help me out


I create a method
C#
public void insert_blog(ref string connection_string, ref Int64 blog_id, ref string title, ref string intro, ref string text, ref string keyword, ref byte categotry, out byte affected_rows)
   {
       affected_rows = 0;

       SqlConnection conn = new SqlConnection(connection_string);
       SqlCommand cmd = new SqlCommand();

       if (conn.State == ConnectionState.Closed)
       {
           conn.Open();
       }
       cmd.CommandText = "insert_blog";
       cmd.Connection = conn;
       cmd.CommandType = CommandType.StoredProcedure;

       cmd.Parameters.Add("@blogid", SqlDbType.BigInt).Value = blog_id;
       cmd.Parameters.Add("@intro", SqlDbType.VarChar).Value = intro;
       cmd.Parameters.Add("@title", SqlDbType.VarChar).Value = title;
       cmd.Parameters.Add("@text", SqlDbType.NVarChar).Value = text;
       cmd.Parameters.Add("@keyword", SqlDbType.VarChar).Value = keyword;
       cmd.Parameters.Add("@category", SqlDbType.TinyInt).Value = categotry;

       affected_rows = Convert.ToByte(cmd.ExecuteNonQuery());


       conn.Close();
       conn.Dispose();
   }


and


C#
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["blog"].ToString();
        blog_class cla = new blog_class();
        string title = TextBox1_title.Text;
        string intro = TextBox1_intro.Text;
        string meta = TextBox1_meta_tags.Text;
        string emp = "";
        Int64 blog_id = 0;
        order_by_tag_photo_ids_wall(ref connection, out blog_id);
        blog_id = blog_id + 1;
        byte cat_id = Convert.ToByte(dropdown1.SelectedValue.ToString());
        byte n;
        cla.insert_blog(ref connection, ref blog_id, ref title, ref intro, ref emp, ref meta, ref cat_id, out n);
        if (n > 0)
        {
            Response.Redirect("~/user/redirect/edit_blog.aspx?blogid=" + blog_id);
        }
        else
        {
            Label1.Text = "Error Occured";
        }




How I upload Image I create a field in database

img varchar(300)
Posted
Updated 22-Dec-14 23:50pm
v2
Comments
Sinisa Hajnal 23-Dec-14 5:20am    
Why all ref parameters? You're not using them for anything.
Arora1992 23-Dec-14 7:32am    
I try to upload Image
Please check this code
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string str = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//uploads//" + str);
string path = "~//uploads//" + str.ToString();
SqlConnection con = new SqlConnection(@"Data Source=LUVLY;Initial Catalog=practice;User ID=sa;Password=lenovo@345");
con.Open();
SqlCommand cmd = new SqlCommand("insert into testing values('" + TextBox1.Text + "','" + path + "')", con);
cmd.ExecuteNonQuery();
con.Close();
Label1.Text = "completed";
SqlDataAdapter da = new SqlDataAdapter("select * from testing",con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
DataBind();
}
else
{
Label1.Text = "Please try again";
}

}

And Html Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
<asp:Label ID="Label1" runat="server" Text="Label">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<columns>
<asp:TemplateField HeaderText="Images">
<itemtemplate>
<asp:Image ID="image" runat="server" ImageUrl='<%Eval("image") %>' Height="1250px" Width="850px" />





</form>
</body>
</html>


Image save into database But when i retrieve It doesnot show the image

Hello,

Please have a look at these Code Project articles. They should help you resolve the issue. Using varchar(300) is not a good idea unless your images are pretty small & that field definition will not allow you to store images in binary format.

Regards,
 
Share this answer
 
use image as a data type in sql
protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
    {
        byte[] img = new byte[FileUpload1.PostedFile.ContentLength];
        HttpPostedFile Image = FileUpload1.PostedFile;
        Image.InputStream.Read(img, 0, (int)FileUpload1.PostedFile.ContentLength);
        string str = "insert img values(@img)";
        SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=aadarsh;user id=sa;password=cos123");
        cn.Open();
        SqlCommand cmd = new SqlCommand(str, cn);
        cmd.Parameters.AddWithValue("@img", img);
        SqlDataReader dr = cmd.ExecuteReader();
        cn.Close();
    }
}
 
Share this answer
 
v2
SqlConnection con = new SqlConnection(@"Data Source=LUVLY;Initial Catalog=practice;User ID=sa;Password=lenovo@345");
con.Open();
string cmdstr = "select * from testing";
SqlCommand cmd = new SqlCommand(cmdstr, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;

GridView1.DataBind();
 
Share this answer
 
C#
if (FileUpload1.HasFile)
     {
         try
         {
             filename = System.IO.Path.GetFileName(FileUpload1.FileName);
             FileUpload1.SaveAs(Server.MapPath("images/") + filename);
             photo = "images/" + filename;
             //Label8.Text = "Upload status: File uploaded!";
         }
         catch (Exception ex)
         {
             photo = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
             //Label8.Text
         }
 
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