3
Answers

insert data with image into database

Hello Everyone..!
 
this is your friend Md Aamir
 
so let's point to the problem is that bfeore image option data waz being inserted but then i using image option so data is not being inserted m i doing wrong plz give solution if i m
i will be your thankfull
 
here you go for my cs code:
  1. protected void btnbut_Click(object sender, EventArgs e)  
  2. {  
  3. if (imageupload.HasFile)  
  4. {  
  5. string strpath = System.IO.Path.GetExtension(imageupload.FileName);  
  6. if (strpath != ".jpg" || strpath != ".jpeg" || strpath != ".gif" || strpath != ".png" || strpath != ".PNG" || strpath != ".GIF" || strpath != ".JPEG" || strpath != ".JPG")  
  7. {  
  8. // lblimgupl.Text = "Only jpg, png, gif,jpg or caps are accepted";  
  9. // ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Only image formats ('jpg, png, gif,jpg or caps are accepted ')", true);  
  10. }  
  11. else  
  12. {  
  13. con.Open();  
  14. SqlCommand cmd = new SqlCommand("insert2", con);  
  15. cmd.CommandType = CommandType.StoredProcedure;  
  16. cmd.Parameters.AddWithValue("@category", ddlcategory.SelectedValue);  
  17. cmd.Parameters.AddWithValue("@name", txtname.Text);  
  18. cmd.Parameters.AddWithValue("@color", txtcolor.Text);  
  19. cmd.Parameters.AddWithValue("@price", txtprice.Text);  
  20. string filename = DateTime.Now.Date.ToString("ddMMyyyy") + DateTime.Now.Minute + DateTime.Now.Millisecond + Path.GetExtension(imageupload.FileName);  
  21. imageupload.SaveAs(Server.MapPath("~/Upload/") + filename);  
  22. cmd.Parameters.AddWithValue("@Image1", filename);  
  23. lblimgupl.Text = "Image successfully uploaded ";  
  24. cmd.ExecuteNonQuery();  
  25. con.Close();  
  26. fill_grd();  
  27. }  
  28. }  
  29. }  
here aspx page: -
  1. <%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/admin/adminMasterPage.master" CodeFile="AddProducts.aspx.cs" Inherits="admin_AddProducts" %>  
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">  
  3. </asp:Content>  
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">  
  5. <asp:MultiView ID="MultiView1" runat="server">  
  6. <asp:View ID="View1" runat="server">  
  7. <div>  
  8. <asp:LinkButton ID="lbviewlist" runat="server" OnClick="lbviewlist_Click">View Product List</asp:LinkButton>  
  9. </div>  
  10. <div>  
  11. Select a Category  
  12. </div>  
  13. <div>  
  14. <asp:DropDownList ID="ddlcategory" runat="server"></asp:DropDownList>  
  15. </div>  
  16. <div>Name:</div>  
  17. <div>  
  18. <asp:TextBox ID="txtname" runat="server"></asp:TextBox>  
  19. </div>  
  20. <div>Color</div>  
  21. <div>  
  22. <asp:TextBox ID="txtcolor" runat="server"></asp:TextBox>  
  23. </div>  
  24. <div>Price:</div>  
  25. <div>  
  26. <asp:TextBox ID="txtprice" runat="server"></asp:TextBox>  
  27. </div>  
  28. <div>  
  29. Select Image:  
  30. </div>  
  31. <div>  
  32. <asp:FileUpload ID="imageupload" runat="server" />  
  33. </div>  
  34. <div>  
  35. <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>  
  36. </div>  
  37. <div>  
  38. <asp:Label ID="lblimgupl" runat="server" ForeColor="Blue"></asp:Label>  
  39. </div>  
  40. <div>  
  41. <asp:Button ID="btnbut" runat="server" Text="Save" OnClick="btnbut_Click" Width="72px" />  
  42. </div>  
  43. </asp:View>  
  44. <asp:View ID="View2" runat="server">  
  45. <div>  
  46. <asp:LinkButton ID="lbback" runat="server" OnClick="lbback_Click">Back</asp:LinkButton>  
  47. </div>  
  48. <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" OnRowEditing="grd_RowEditing" OnRowUpdating="grd_RowUpdating" OnRowCommand="grd_RowCommand" OnSelectedIndexChanged="grd_SelectedIndexChanged">  
  49. <Columns>  
  50. <asp:BoundField DataField="pid" HeaderText="ID" />  
  51. <asp:TemplateField HeaderText="Cetagory">  
  52. <ItemTemplate>  
  53. <%#Eval("category") %>  
  54. </ItemTemplate>  
  55. </asp:TemplateField>  
  56. <asp:TemplateField HeaderText="Name">  
  57. <ItemTemplate>  
  58. <%#Eval("name") %>  
  59. </ItemTemplate>  
  60. </asp:TemplateField>  
  61. <asp:TemplateField HeaderText="Color">  
  62. <ItemTemplate>  
  63. <%#Eval("color") %>  
  64. </ItemTemplate>  
  65. </asp:TemplateField>  
  66. <asp:TemplateField HeaderText="Price">  
  67. <ItemTemplate>  
  68. <%#Eval("price") %>  
  69. </ItemTemplate>  
  70. </asp:TemplateField>  
  71. <asp:TemplateField HeaderText="Click_it">  
  72. <ItemTemplate>  
  73. <asp:LinkButton ID="lnkbtndelt" runat="server" CommandName="delt" CommandArgument='<%#Eval("pid") %>' Text="Delet" />  
  74. </ItemTemplate>  
  75. </asp:TemplateField>  
  76. <asp:TemplateField HeaderText="Click_It">  
  77. <ItemTemplate>  
  78. <asp:LinkButton ID="lnkbtnedit" runat="server" CommandName="edt" CommandArgument='<%#Eval("pid") %>' Text="Edit" OnClick="btnedit_Click" />  
  79. </ItemTemplate>  
  80. </asp:TemplateField>  
  81. <asp:TemplateField HeaderText="Click_It">  
  82. <ItemTemplate>  
  83. <asp:LinkButton ID="lnkbtnupd" runat="server" CommandName="upd" CommandArgument='<%#Eval("pid") %>' Text="update" OnClick="lnkbtnupd_Click" />  
  84. </ItemTemplate>  
  85. </asp:TemplateField>  
  86. <asp:TemplateField HeaderText="Status">  
  87. <ItemTemplate>  
  88. <asp:LinkButton ID="lbtactive" runat="server" Text="Active" Visible="false" CommandName="Active" OnClick="lbtactive_Click"></asp:LinkButton>  
  89. <asp:LinkButton ID="ibinactive" runat="server" Text="Inactive" CommandName="Inactive" OnClick="ibinactive_Click"></asp:LinkButton>  
  90. </ItemTemplate>  
  91. </asp:TemplateField>  
  92. </Columns>  
  93. </asp:GridView>  
  94. </asp:View>  
  95. <asp:View ID="View3" runat="server">  
  96. <div>ID:<asp:Label ID="lblID" runat="server" ForeColor="#CC3300" BorderColor="Red"></asp:Label></div>  
  97. <div>  
  98. <asp:Label ID="lblmsg" runat="server" ForeColor="Blue"></asp:Label>  
  99. </div>  
  100. <div>  
  101. <asp:LinkButton ID="LinkButton1" runat="server" OnClick="lbviewlist_Click">Back</asp:LinkButton>  
  102. </div>  
  103. <div>  
  104. Select A Category  
  105. </div>  
  106. <div>  
  107. <asp:DropDownList ID="ddlcetagory1" runat="server"></asp:DropDownList>  
  108. </div>  
  109. <div>Name:</div>  
  110. <div>  
  111. <asp:TextBox ID="txtname1" runat="server"></asp:TextBox>  
  112. </div>  
  113. <div>Color</div>  
  114. <div>  
  115. <asp:TextBox ID="txtcolor1" runat="server"></asp:TextBox>  
  116. </div>  
  117. <div>Price:</div>  
  118. <div>  
  119. <asp:TextBox ID="txtprice1" runat="server"></asp:TextBox>  
  120. </div>  
  121. <div>  
  122. <asp:Button ID="BtnSave" runat="server" Text="Save" OnClick="BtnSave_Click" Width="72px" />  
  123. </div>  
  124. </asp:View>  
  125. </asp:MultiView>  
  126. </asp:Content>  

Answers (3)