ASP.NET C#在DataBase中存储Session

时间:2016-06-11 20:02:28

标签: c# mysql asp.net session

我是ASP.NET和C#的新手。我试图将预订的详细信息存储到预订表中,包括使用会话的用户ID。 我的预订表如下

CREATE TABLE [dbo].[BookingTb] (
[Bid]       INT          IDENTITY (1, 1) NOT NULL,
[Rid]       VARCHAR (5)  NOT NULL,
[Price]     MONEY        NOT NULL,
[nights]    INT          NOT NULL,
[NoOfRooms] INT          NOT NULL,
[Uid] VARCHAR(50) NOT NULL, 
PRIMARY KEY CLUSTERED ([Bid] ASC));

,用户表如下

CREATE TABLE [dbo].[UserTb] (
[Uid]      VARCHAR (50) NOT NULL,
[Uname]    VARCHAR (15) NOT NULL,
[Ubd]      INT          NOT NULL,
[Ugender]  VARCHAR (10) NOT NULL,
[Uemail]   VARCHAR (50) NOT NULL,
[password] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Uid] ASC));

我在登录表单中创建了会话,如下所示

Session["Uid"] = TextBox1.Text.ToString();

现在我想在Booking表中存储不同的细节,包括用户登录后的会话,所以我使用了以下代码:

using System.Data;
using System.Data.SqlClient;
public partial class UserBooking : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    protected void Button1_Click(object sender, EventArgs e)
    {
        BookingTb b = new BookingTb();
        Int32 rooms,nights, cap,price;
        rooms = Convert.ToInt32(TextBox1.Text);
        nights = Convert.ToInt32(TextBox2.Text);
        cap = Convert.ToInt32(DropDownList2.SelectedItem.Value);
        String type = DropDownList1.SelectedItem.Value;
        String Rid;
        String user =Convert.ToString(Session["Uid"]) ;

        if (type == "Normal" && cap == 2)
        { 
            Rid = "G01";
            price = rooms * nights * 35;
          b.Rid = Rid;
            b.Price = price;
            b.nights = nights;
            b.Uid = user;
            b.NoOfRooms = rooms;
            db.BookingTbs.InsertOnSubmit(b);
            db.SubmitChanges();
            Label3.Text = "Thank you for Booking with us!</br> Total Payment= " + price;
        }

问题是我一直收到这个错误:

    Line 39:b.Uid = user;
Compiler Error Message: CS0029: Cannot implicitly convert type 'string' to 'int'

嗯,Uid是varchar(50)而不是int!我该怎么解决?我试过很多方法,但没有办法。

1 个答案:

答案 0 :(得分:0)

在数据库中,Uid是字符串类型,您的会话转换是正确的,但

 BookingTb b = new BookingTb();

在这个类中你已经将UId类型设置为int,得到了这个类并根据数据库将Uid属性设置为string而不是int