[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.2

help with adding an image to SQL database (using c# and ASP)

Asked by SASKPEN in Programming for ASP.NET, C# Programming Language, SQL Server 2008

Tags: c#, ASP, SQL server 2008

Hello all,
    I am designing a website that a user can add an item to a classified section. I have put a lot of work into this site but....I have reached a stand still. grrrrr
Anyways, here's my problem (i think)
 I am trying very desperately to add an image along with some other data  to my database--but nothing is being added. There are no errors (that I know of) But it kicks me to my error page without adding anything to the database. The only thing I can figure out is that  I have completed the "image" section wrong.

The items are not getting input into the database
Yet....there are no major errors found. it just shoots off to my error page and does not add anything to the database.
 
   * all images are declared as an object now....yet I am still trying to upload to UploadImages folder.
   * Is this right?  
   * even if I am not uploading an image -- it still does not work.
   * Could this give me issues of trying to get stuff in my db?

I have never actually added images in this way. (could be a big issue, could be a small issue...hey, it may not even be my issue - who knows? Do you? Can you help me please?)

Do I have to add something to my web config file?? The only thing I have actually changed/added is my connection string.

Thanks in advance for your help....I really appreciate it.
Darci
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
//classified.cs
 
//without putting all the code here....I'll just put some relevant info.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Classifieds.Data;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data.SqlTypes;
 
namespace Classifieds.Data
{
 public class classifieds
    {
       
        private static readonly string _connectionString = WebConfigurationManager.ConnectionStrings["DiscoverPAConnectionString1"].ConnectionString;
 object image;
 
  public classifieds() { }
 
 
        public classifieds(string ItemDescription, object Image, string Title, decimal Price, string Email, string Phone, string CategoryID, string UserID)
        {
this.Image = image;
}
 
 
 public object Image
        {
            get { return image; }
            set { image = value; }
        }
 
//addclassifieds.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Classifieds.Data;
using System.Web.Configuration;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI;
 
namespace Classifieds.Data
{
 
 
    public class addClassifieds
    {
        public int Count;
        private List<classifieds> _Items;
        private static readonly string _connectionString = WebConfigurationManager.ConnectionStrings["DiscoverPAConnectionString1"].ConnectionString;
 
        public addClassifieds()
        {
            this.Items = new List<classifieds>();
        }
 
        public List<classifieds> Items
        {
        set{this._Items = value;}
        get{return this._Items;}
            
        }
 public List<classifieds> GetAll()
        {
            List<classifieds> temp = new List<classifieds>();
            SqlConnection con = new SqlConnection(_connectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection  = con;
            cmd.CommandText = "Select * From [classifieds]";
            
 
                SqlDataReader sd = cmd.ExecuteReader(CommandBehavior.CloseConnection );
  
                while(sd.Read())
                {
                    classifieds c = new classifieds ();              
               
                     c.ItemDescription = (string)sd["ItemDescription"]; 
                     c.Image = (object)sd["Image"]; 
                     c.Title = (string)sd["Title"];
                     c.Price = (decimal)sd["Price"]; 
                     c.Email = (string)sd["Email"]; 
                     c.Phone = (string)sd["Phone"];
                     c.CategoryID = (string)sd["CategoryID"];                   
                     c.UserID = (string)sd["UserID"];
              
                    temp.Add(c); 
                }               
                return temp;
        }
 public  bool AddData(classifieds c)
        {
 
            bool newItem = true;
            SqlConnection con = new SqlConnection(_connectionString);
            SqlCommand cmd = new SqlCommand();
            // cmd = new SqlCommand("SELECT * FROM classifieds WHERE itemDescription=@itemDescription", con);
            //cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            //  SqlDataReader reader;
            cmd.CommandText = "SELECT * FROM classifieds where UserID = @UserID";
            cmd.Parameters.AddWithValue("@UserID", c.UserID);
            
          
            cmd.CommandText = "INSERT INTO classifieds (ItemDescription, Image, Title, Price, Email, Phone, CategoryID, UserID) Values (@ItemID, @ItemDescription, @Image, @Title, @Price, @Email, @Phone, @CategoryID, @UserID)";
 
          //  cmd.Parameters.AddWithValue("ItemID", c.ItemID);
            cmd.Parameters.AddWithValue("@ItemDescription", c.ItemDescription);
            cmd.Parameters.AddWithValue("@Image", c.Image);
            cmd.Parameters.AddWithValue("@Title", c.Title);
            cmd.Parameters.AddWithValue("@Price", c.Price);
            cmd.Parameters.AddWithValue("@Email", c.Email);
            cmd.Parameters.AddWithValue("@Phone", c.Phone);
            cmd.Parameters.AddWithValue("@CategoryID", c.CategoryID);
            cmd.Parameters.AddWithValue("@UserID", c.UserID);
          
 
            con.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                newItem = false;
            }
            //  reader = cmd.ExecuteReader();
 
            con.Close();
            return newItem;
        }
 
//addclassified.aspx.cs
//I added the whole file since my issue may be within here
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using Classifieds.Data;
using System.IO;
 
 
public partial class Design_addClassified : System.Web.UI.Page
{
    int tries = -1;
    string UserN;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["tries"] = tries;
        tries++;
 
        if (tries > 3)
        {
            Response.Redirect("enter.aspx");
        }
    }
    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
 
    }    
    protected void Button2_Click(object sender, EventArgs e)
    {
        txtUserID.Text = "";
        txtEmail.Text = "";
        txtPrice.Text = "";
        txtDescription.Text = "";
        txtTitle.Text = "";
        txtPrice.Text = "";
        txtPhone.Text = "";
        DropDownList1.Text = "";
     
    }
    protected void btnAddItem_Click(object sender, EventArgs e)
    {
     
        string UserID = txtUserID.Text;
        object Image = upImage.FileName;   
       // Stream Image = upImage.FileName;
       // Stream Image = upImage.PostedFile.InputStream;
        string Title = txtTitle.Text;    
        decimal Price = Convert.ToDecimal(txtPrice.Text);
        string ItemDescription = txtDescription.Text;
        string Email = txtEmail.Text;
        string Phone = txtPhone.Text;  
        string CategoryID = DropDownList1.SelectedValue.ToString();
        SqlDataSource DataSource = (SqlDataSource)btnAddItem.FindControl("SqlDataSource1");
 
        classifieds c = new classifieds(ItemDescription, Image, Title, Price, Email, Phone, CategoryID, UserID);
        addClassifieds ac = new addClassifieds();
 
        if (ac.AddData(c) == true)
        {
            if (Session["UserN"] != null)
            {
                UserN = (string)Session["UserN"];
            }
            else
            {
                UserN = txtUserID.Text;
                Session["UserN"] = UserN;
            }
             
            if (upImage.HasFile)
            {
                if (checkTheFileType(upImage.FileName))
                {
                    String filePath = "~/Design/UploadImages/" + upImage.FileName;
                    upImage.SaveAs(MapPath(filePath));
                }
            }
            classifieds cfds = new classifieds(ItemDescription, Image, Title, Price, Email, Phone, CategoryID, UserID);
            ac.AddData(cfds);            
            Response.Redirect("~/Design/viewclassifieds3.aspx");
        }
        else
        {
            Response.Redirect("~/Design/error.aspx");
        }
    }  
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (upImage.HasFile)
        {
            if (checkTheFileType(upImage.FileName))
            {
                String filePath = "~/Design/UploadImages/" + upImage.FileName;
                upImage.SaveAs(MapPath(filePath));
            }
        }
    }
    bool checkTheFileType(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        //string ext = (MapPath(filePath(fileName)));
        switch (ext.ToLower())
        {
            case ".gif":
                return true;
            case ".png":
                return true;
            case ".jpg":
                return true;
            case ".jpeg":
                return true;
            default:
                return false;
        }
    } 
}
 
//addclassified.aspx
 
 
 
 
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                           ConnectionString="<%$ ConnectionStrings:DiscoverPAConnectionString1 %>" 
                           ProviderName="<%$ ConnectionStrings:DiscoverPAConnectionString1.ProviderName %>" 
                           
                           SelectCommand="SELECT [itemID], [itemDescription], [image], [title], [price], [email], [phone], [categoryID], [userID] FROM [classifieds] WHERE (([userID] = @userID) AND ([itemID] = @itemID))">
                           <SelectParameters>
                               <asp:ControlParameter ControlID="lblUserID" Name="userID" PropertyName="Text" 
                                   Type="String" />
                               <asp:SessionParameter Name="UserID" SessionField="@UserID" Type="Int32" />
                           </SelectParameters>
                       </asp:SqlDataSource>
                   </td>
               </tr>
               <tr>
                   <td>
                       <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                           DataObjectTypeName="Classifieds.Data.classifieds" InsertMethod="AddData" 
                           SelectMethod="AddData" TypeName="Classifieds.Data.addClassifieds">
                           <SelectParameters>
                              <asp:SessionParameter Name="ItemID" SessionField="@ItemID" Type="String" />
                                <asp:SessionParameter Name="ItemDescription" SessionField="@ItemDescription" Type="String" />
                                  <asp:SessionParameter Name="Image" SessionField="@Image" Type="String" />
                                    <asp:SessionParameter Name="Title" SessionField="@Title" Type="String" />
                                      <asp:SessionParameter Name="Price" SessionField="@Price" Type="String" />
                                        <asp:SessionParameter Name="Email" SessionField="@Email" Type="String" />
                                          <asp:SessionParameter Name="Phone" SessionField="@Phone" Type="String" />
                                            <asp:SessionParameter Name="CategoryID" SessionField="@CategoryID" Type="String" />
                             <asp:SessionParameter Name="UserID" SessionField="@UserID" Type="String" />
                              
                           </SelectParameters>
                       </asp:ObjectDataSource>
[+][-]10/08/09 09:27 PM, ID: 25532287Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Programming for ASP.NET, C# Programming Language, SQL Server 2008
Tags: c#, ASP, SQL server 2008
Sign Up Now!
Solution Provided By: vinurajr
Participating Experts: 3
Solution Grade: A
 
[+][-]10/08/09 09:54 PM, ID: 25532401Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/08/09 09:57 PM, ID: 25532413Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/09/09 01:09 AM, ID: 25533069Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/03/09 02:45 PM, ID: 25734699Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]11/03/09 03:42 PM, ID: 25735132Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/16/09 06:05 PM, ID: 25836242Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_3_20080625