1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Check if column value in sql table is null in asp.net c#

Discussão em 'Outras Linguagens' iniciado por Stack, Julho 21, 2021.

  1. Stack

    Stack Membro Participativo

    I am working in asp.net in C# and I am struggling to finish the piece of code. A dropdownlist is displayed on my page on which the user can give a rating for a book on the page. First, I want to check if the user already gave a rating or not (if the rating column has a value) If it doesn't have a value, the user can then do the rating by selecting from the dropwdownlist.

    Here is my code so far. I am unsure what to write within the if()

    //CRUD statement
    SqlCommand cmdCheck = ratingconn.CreateCommand();
    cmdCheck.CommandText = "SELECT bookRating from tbl_ratingInfo WHERE userID = '" + Session["userID"] + "'";

    if()
    {
    //reading the information from the database
    SqlDataReader reader = cmdCheck.ExecuteReader();
    if (reader.Read())
    {
    // setting the label text values
    ddl_BookName.Text = reader.GetInt32(0).ToString();
    }
    }
    else
    {
    //creating CRUD statement
    SqlCommand cmdRating = ratingconn.CreateCommand();
    cmdRating.CommandText = "INSERT INTO tbl_ratingInfo (bookRating) VALUES('"
    + ddl_Rating.Text + "') WHERE userID = " + Session["userID"] + "' ";
    }


    Here is my database. This table is an intersection table in sql code.

    -- Create the rating info table
    CREATE TABLE tbl_ratingInfo
    (
    -- Add Foreign Keys from members and class tables
    bookTitle VARCHAR (100) NOT NULL REFERENCES tbl_bookInfo(bookTitle),
    userID INT NOT NULL REFERENCES tbl_userInfo(userID),
    bookRating INT NOT NULL DEFAULT 5 CHECK (bookRating <= 5),
    -- Composite Primary Key
    PRIMARY KEY (bookTitle, userID)
    )
    GO

    Continue reading...

Compartilhe esta Página