Visual Studio网页代码中的错误(尝试将网站连接到数据库)

艾莎·艾哈迈德·艾哈迈德(Aisha Ahmed Ahmed)

我正在尝试在(Visual Studio)上创建一个网站并将其连接到数据库(Microsoft SQL Server 2014),首先我正在从youtube上的视频中学习该教程是关于一个咖啡网站和数据库的

我有一些错误,但我不知道问题出在哪里

此页面中的错误Coffe.aspx.cs

这是页面(Coffee.aspx.cs)代码

using System;
using System.Collections;
using System.Text;

namespace Pages
{
    public partial class Pages_Coffee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillPage();
        }

        private void FillPage()
        {
            ArrayList coffeeList = ConnectionClass.GetCoffeeByType(DropDownList1.SelectedValue);
            StringBuilder sb = new StringBuilder();

            foreach (Coffee coffee in coffeeList)
            {
                sb.Append(
                    string.Format(
                        @"<table class='coffeeTable'>
            <tr>
                <th rowspan='6' width='150px'><img runat='server' src='{6}' /></th>
                <th width='50px'>Name: </td>
                <td>{0}</td>
            </tr>

            <tr>
                <th>Type: </th>
                <td>{1}</td>
            </tr>

            <tr>
                <th>Price: </th>
                <td>{2} $</td>
            </tr>

            <tr>
                <th>Roast: </th>
                <td>{3}</td>
            </tr>

            <tr>
                <th>Origin: </th>
                <td>{4}</td>
            </tr>

            <tr>
                <td colspan='2'>{5}</td>
            </tr>           

           </table>", coffee.Name, coffee.Type, coffee.Price, coffee.Roast, coffee.Country, coffee.Review, coffee.Image));
                lblOuput.Text = sb.ToString();
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillPage();
        }
    }
}

这也是页面(Coffee.aspx)代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Masterpage.master" AutoEventWireup="true" CodeFile="Coffee.aspx.cs" Inherits="Coffee" %>

<script runat="server">

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
</script>


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        Select a type:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="sds_type" DataTextField="type" DataValueField="type" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:SqlDataSource ID="sds_type" runat="server" ConnectionString="<%$ ConnectionStrings:CoffeeDBConnectionString %>" SelectCommand="SELECT DISTINCT [type] FROM [coffee] ORDER BY [type]"></asp:SqlDataSource>
    </p>
    <p>
        <asp:Label ID="lblOutput" runat="server" Text="Label"></asp:Label>
    </p>
</asp:Content>

这是(ConnectionClass.cs)代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;

public static class ConnectionClass
{
    private static SqlConnection conn;
    private static SqlCommand command;

    static ConnectionClass()
    {
        string connectionString = 
            ConfigurationManager.ConnectionStrings["coffeeConnection"].ToString();
        conn = new SqlConnection(connectionString);
        command = new SqlCommand("", conn);
    }

    public static ArrayList GetCoffeeByType(string coffeeType)
    {
        ArrayList list = new ArrayList();
        string query = string.Format("SELECT * FROM coffee WHERE type LIKE '{0}'", coffeeType);

        try
        {
            conn.Open();
            command.CommandText = query;
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                int id = reader.GetInt32(0);
                string name = reader.GetString(1);
                string type = reader.GetString(2);
                double price = reader.GetDouble(3);
                string roast = reader.GetString(4);
                string country = reader.GetString(5);
                string image = reader.GetString(6);
                string review = reader.GetString(7);

                Coffee coffee = new Coffee(id, name, type, price, roast, country, image, review);
                list.Add(coffee);
            }
        }
        finally
        {
            conn.Close();
        }

        return list;
    }

}

(web.confg)代码

    <?xml version="1.0"?>
    <configuration>
      <appSettings/>

      <connectionStrings>
        <clear/>
        <add name="coffeeConnection" 
             connectionString="Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=CoffeeDB;Integrated Security=True" 
             providerName="System.Data.SqlClient"/>
      </connectionStrings>

      <system.web>
        <compilation debug="true" targetFramework="4.0">
          <assemblies>
            <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
          </assemblies>
        </compilation>
        <authentication mode="Windows"/>
      </system.web>

    </configuration>

and (Coffee.cs)code

public class Coffee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public double Price { get; set; }
    public string Roast { get; set; }
    public string Country { get; set; }
    public string Image { get; set; }
    public string Review { get; set; }

    public Coffee(int id, string name, string type, double price, string roast, string country, string image, string review)
    {
        Id = id;
        Name = name;
        Type = type;
        Price = price;
        Roast = roast;
        Country = country;
        Image = image;
        Review = review;
    }
}

最后是SQL代码

GO
CREATE TABLE [dbo].[coffee](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NOT NULL,
    [type] [varchar](50) NOT NULL,
    [price] [float] NOT NULL,
    [roast] [varchar](50) NOT NULL,
    [country] [varchar](50) NOT NULL,
    [image] [varchar](255) NULL,
    [review] [text] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[coffee] ON
INSERT [dbo].[coffee] ([id], [name], [type], [price], [roast], [country], [image], [review]) VALUES (1, N'Café au Lait', N'Classic', 2.25, N'Medium', N'France', N'../Images/Coffee/Cafe-Au-Lait.jpg', N'A coffee beverage consisting strong or bold coffee (sometimes espresso) mixed with scalded milk in approximately a 1:1 ratio.')
INSERT [dbo].[coffee] ([id], [name], [type], [price], [roast], [country], [image], [review]) VALUES (2, N'Caffè Americano', N'Espresso', 2.25, N'Medium', N'Italy', N'../Images/coffee/caffe_americano.jpg', N'Similar in strength and taste to American-style brewed coffee, there are subtle differences achieved by pulling a fresh shot of espresso for the beverage base.')
INSERT [dbo].[coffee] ([id], [name], [type], [price], [roast], [country], [image], [review]) VALUES (3, N'Peppermint White Chocolate Mocha', N'Espresso', 3.25, N'Medium', N'Italy', N'../Images/coffee/white-chocolate-peppermint-mocha.jpg', N'Espresso with white chocolate and peppermint flavored syrups.
Topped with sweetened whipped cream and dark chocolate curls.')
INSERT [dbo].[coffee] ([id], [name], [type], [price], [roast], [country], [image], [review]) VALUES (4, N'Irish Coffee', N'Alcoholic', 2.25, N'Dark', N'Ireland', N'../Images/coffee/irish coffee.jpg', N'A cocktail consisting of hot coffee, Irish whiskey, and sugar, stirred, and topped with thick cream. The coffee is drunk through the cream.')
SET IDENTITY_INSERT [dbo].[coffee] OFF

我仍然是初学者~~我需要了解这些错误以及如何解决这些错误

杰里米·哈钦森(Jeremy Hutchinson)

好的,看起来您创建了页面名称Coffee,然后将代码隐藏文件中的类重命名为Page_Coffee,或者反之亦然。不管哪种方式,问题是在Coffee.aspx文件中有此文件

CodeFile="Coffee.aspx.cs" Inherits="Coffee"

但是,在Coffee.aspx.cs文件中,您需要声明这样的类

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

如果将类名和继承名同步,它将解决您遇到的编译错误。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将查询连接到 Visual Studio 中的数据库错误

无法将MySQL数据库连接到Visual Studio?

如何在Visual Studio中使用Razor网页设置数据库并连接到数据库?

Visual Studio代码映射:无法连接到指定的数据库

无法从Visual Studio连接到Azure SQL数据库

从Visual Studio COBOL连接到MySQL数据库

在Visual Studio connectionString内连接到SQL Server数据库的错误

将Visual Basic连接到本地数据库

将Visual Studio Code连接到远程Mysql数据库

无法将 Visual Studio 连接到 SQL Server 数据库

手动将 MS Access 数据库连接到 Visual Studio

如何将Oracle数据库连接到Visual Studio C#项目

如何从 Visual Studio 中的网页访问数据库?

连接mongoose数据库Visual Studio代码

连接到Visual Studio中的数据库以获取会员资格

如何在Visual Studio中连接Oracle数据库

在Visual Studio 2015中创建代码优先的数据库

Visual Studio尝试在加载解决方案时连接SQL数据库。给出错误:无法打开登录请求的数据库

使用Active Directory身份验证连接到Visual Studio Code中的Azure Sql数据库

如何使用Visual Studio 2013连接到asp.net中的本地SQL Server数据库?

从Visual Studio 2013连接到SQL Server 2008 R2数据库

连接到远程SQL Server数据库Visual Studio 2017时出错

在Microsoft Visual Studio中使用Python连接到数据库

无法通过 Visual Studio 2012 连接到 MySQL 数据库

.net核心未连接到Visual Studio调试中appsettings.json连接字符串中已更改的数据库

将Visual Studio代码(VSCode)连接到VirtualBox VM

Visual Studio-无法将MySql数据库表链接到DataSet

将Visual Studio项目重新连接到GitHub存储库

将数据连接更新到 Visual Studio 17 上的最新数据库版本