使用参数检索汽车的型号和价格

时间:2018-11-16 12:28:48

标签: c# asp.net sql-server visual-studio express

using System;
using System.Data.SqlClient;

namespace car_database
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public class car
        {
            string connstring = "Data Source=.\\SqlExpress; Initial Catalog=carsale; Integrated Security=True";

            protected BtnView(object sender, EventArgs e)
            {
                SqlConnection myConnection = new SqlConnection(connstring);

                try
                {
                    myConnection.Open();
                    string query = "select model ,price from car where Id =@carid";

                    SqlCommand mycmd = new SqlCommand(query, myConnection);

                    int carid1;

                    if (!int.TryParse(TextBox1.Text, out carid1))
                    { 
                         Label1.Text = "please enter a numeric ID!";
                    }
                    else
                    {
                        mycmd.Parameters.Add("@carid", System.Data.SqlDbType.Int);
                        mycmd.Parameters["@carid"].Value = carid1;

                        SqlDataReader reader = mycmd.ExecuteReader();

                        if (reader.Read())
                        {
                            Label1.Text = "Model:" + reader["model"] + "<br/>" + "price:" + (string)reader["price"];
                        }
                        else
                        {
                            Label1.Text = "no car has this id ";
                        }

                        reader.Close();
                    }
                }
                catch (Exception ex)
                {
                    Label1.ForeColor = System.Drawing.Color.Red;
                    Label1.Text = ex.Message;
                }
                finally
                {
                    myConnection.Close();
                }
            }
        }
    }
}

当我根据在TextBox1中输入的ID单击按钮时,我想在Label1中显示汽车型号和价格。

aspx标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="car_database.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
enter id to retrieve the model and the price of the car :
<asp:TextBox runat="server" ID="TextBox1" />

            <br />
            <asp:Button runat="server" ID="btn" OnClick ="BtnView" />
            <br />
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </div>
    </form>
</body>
</html>

但是代码不起作用-我遇到了一些错误:

  

公共类汽车必须具有返回类型

TextBox1.Text和Label1.Text出现错误:

  

非静态字段方法或属性需要对象引用

2 个答案:

答案 0 :(得分:1)

我看到错误的夫妇

protected BtnView(object sender, EventArgs e)
{

应该是

protected Void BtnView(object sender, EventArgs e) 
{
   ....
}

另外,考虑到Label1.Text = "please enter a numeric ID!";是Label控件实例名称,this.Label1.Text = "please enter a numeric ID!";应该是Label1

答案 1 :(得分:0)

删除类公共类汽车,该代码必须在 WebForm1 类中才能访问其控件。

尝试这样:

using System;
using System.Data.SqlClient;


namespace car_database
{
    public partial class WebForm1 : System.Web.UI.Page
    {



            string connstring = "Data Source=.\\SqlExpress; Initial Catalog=carsale; Integrated Security=True";

            protected BtnView(object sender, EventArgs e)
            {
                SqlConnection myConnection = new SqlConnection(connstring);
                try
                {
                    myConnection.Open();
                    string query = "select model ,price from car where Id =@carid";

                    SqlCommand mycmd = new SqlCommand(query, myConnection);

                    int carid1;

                    if (!int.TryParse(TextBox1.Text, out carid1))
                    { Label1.Text = "please enter a numerac ID!"; }

                    else
                    {
                        mycmd.Parameters.Add("@carid", System.Data.SqlDbType.Int);
                        mycmd.Parameters["@carid"].Value = carid1;

                        SqlDataReader reader = mycmd.ExecuteReader();

                        if (reader.Read())
                        {
                            Label1.Text = "Model:" + reader["model"] + "<br/>" + "price:" + (string)reader["price"];

                        }

                        else
                        {
                            Label1.Text = "no car has this id ";

                        }

                        reader.Close();
                    }

                }
                catch (Exception ex)
                {
                    Label1.ForeColor = System.Drawing.Color.Red;
                    Label1.Text = ex.Message;

                }
                finally
                {

                    myConnection.Close();

                }

            }

    }
}