Jump to content

Recommended Posts

Posted

Hi there

 

I am hoping someone can help, I am redoing my mums website as it is broken and rubbish, I am really stuck on one thing and I have done some googling but either I'm just braindead or nothing really useful.

 

I have a page called add images, which has the code

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/FrontEnd.master" AutoEventWireup="true" CodeFile="AddImages.aspx.cs" Inherits="Management_AddImages" %>

 

 

 

 

 

 

         

 

 

 

 

 

 

With the code behind code as

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

 

public partial class Management_AddImages : System.Web.UI.Page

{

atHeathersEntities entities = new atHeathersEntities();

protected void Page_Load(object sender, EventArgs e)

{

 

}

protected void CategoryList_SelectedIndexChanged(object sender, EventArgs e)

{

 

}

public IEnumerable Categorylist_GetData()

{

return from p in entities.Categories orderby p.Id select p;

}

protected void Button1_Click(object sender, EventArgs e)

{

if (FileUpload1.PostedFile != null)

{

string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

string Extension = Path.GetExtension(FileUpload1.FileName);

 

//Save files to disk

FileUpload1.SaveAs(Server.MapPath("~/Images/" + TextBoxName.Text + Extension));

 

//Add Entry to DataBase

String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["atHeathersConnectionString"].ConnectionString;

SqlConnection con = new SqlConnection(strConnString);

string strQuery = "insert into Bathroom (Name, Description, ToolTip, FilePath) values(@Name, @Description, @ToolTip, @CategoryId, @FilePath)";

SqlCommand cmd = new SqlCommand(strQuery);

cmd.Parameters.AddWithValue("@Name", TextBoxName.Text);

cmd.Parameters.AddWithValue("@Description", TextBoxDescription.Text);

cmd.Parameters.AddWithValue("@ToolTip", TextBoxToolTip.Text);

cmd.Parameters.AddWithValue("@CategoryId", CategoryList.SelectedValue);

cmd.Parameters.AddWithValue("@FilePath", "images/" + TextBoxName.Text + Extension);

cmd.CommandType = CommandType.Text;

cmd.Connection = con;

try

{

con.Open();

cmd.ExecuteNonQuery();

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

finally

{

con.Close();

con.Dispose();

}

}

}

}

 

So what im stuck with is the databinding of the dropdown menu, at the moment it gets data from one database table called categories with ID and name in the table. and when the upload button is clicked it should upload the ID of the Name to a new table called Picture, into the CategoryId column.

However everytime I click upload it gives me the error

Invalid object name 'Bathroom'.
(there are currently two names in the category table ID,1:Name,Bathroom ID,2:Name,Bedroom) it uploads the picture fine to the images folder with the correct name, but it doesn't upload anything to the database table Pictures. But if I remove the dropdown list it works fine. So can someone call me an idiot and tell me what simple thing I have done wrong?

 

Thanks and sorry for the long post :(

Posted

In your //Add Entry to DataBase section you have a line that starts:

 

string strQuery = "insert into Bathroom

 

And that's going to try and add a row to the Bathroom table. I reckon you're lacking a Bathroom table.

 

The next problem you will have will relate to this:

 

"insert into Bathroom (Name, Description, ToolTip, FilePath) values(@Name, @description, @ToolTip, @CategoryId, @filePath)";

 

You've specified 4 columns in the table, but you're inserting 5 values.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...