samedi 11 juin 2016

ASP.NET C# storing Session in DataBase

i am new with ASP.NET and C#. I was trying to store details of a booking into the booking table including User ID using the session. My booking table is as follow

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));

and the user table is as follow

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));

i created the session in the login form as follow

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

Now i want to store different details in the Booking table including the session after the user log in so i used the following code:

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;
        }

the problem is i keep getting this error:

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

well, Uid is a varchar(50) not an int! how can i solve that? i tried many ways but non of them worked.

Aucun commentaire:

Enregistrer un commentaire