locked
InsertOnSubmit on Linq - Put it into the database RRS feed

  • Question

  • User-1322569703 posted

    This is how I am going to do so that the users of the website can sustain themselves, but when I try to do like this,

    What I could'm going to happened in the code here was to create the user in the database.

    protected void ButtonOpretbruger_Click(object sender, EventArgs e)
    {
        string fejl = "Hov Hov, Du skal læse vore betingelser";
    
        if (CheckBoxBetingelser.Checked)
        {
            LabelError.Visible = false;
            string brugernavn = System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ToTitleCase(TextBoxBrugernavn.Text);
    
            //checks if username exists in the database.
            var opretbrugerTjekEMail = db.brugeres.FirstOrDefault(brugerEmail => brugerEmail.brugernavn == brugernavn);
            if (opretbrugerTjekEMail == null)
            {
                //Begin to create user in the database.
                opretbrugerTjekEMail.brugernavn = brugernavn;
                opretbrugerTjekEMail.adgangskode = Hash.getHashSha256(TextBoxAdgangskode.Text);
                opretbrugerTjekEMail.fornavn = TextBoxFornavn.Text;
                opretbrugerTjekEMail.efternavn = TextBoxEfternavn.Text;
    
                db.brugeres.InsertOnSubmit(opretbrugerTjekEMail);
    
                db.SubmitChanges();
    
                //send users to login site.
                Response.Redirect("~/login.aspx");
    
            }
            else
            {
                LabelErrorBesked.Text = "Hov hov, denne her email er optaget " + brugernavn;
            }
        }
        else
        {
            //error her
            LabelError.Text = fejl;
        }
    }

    The error I receive looks like this,

    Tuesday, April 14, 2015 2:52 PM

Answers

  • User-824764785 posted

    You need to initialize the Burger object:

     //checks if username exists in the database.
    var opretbrugerTjekEMail = db.brugeres.FirstOrDefault(brugerEmail => brugerEmail.brugernavn == brugernavn);
    if (opretbrugerTjekEMail == null)
    {
        //Begin to create user in the database.
        opretbrugerTjekEMail = new burger(); //first initialize it before setting any values
        opretbrugerTjekEMail.brugernavn = brugernavn;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 14, 2015 3:18 PM
  • User-824764785 posted

    sorry spelling mistake, it should be the table name in which you wanted to add the records

      opretbrugerTjekEMail = new brugere();  //Or the table name which you have added in your dbml file



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 14, 2015 3:30 PM
  • User-824764785 posted

    There must be a colum in sql table with datatype of DateTime set to NOT NULL, you are not passing any value in that column

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2015 2:18 AM

All replies

  • User-824764785 posted

    You need to initialize the Burger object:

     //checks if username exists in the database.
    var opretbrugerTjekEMail = db.brugeres.FirstOrDefault(brugerEmail => brugerEmail.brugernavn == brugernavn);
    if (opretbrugerTjekEMail == null)
    {
        //Begin to create user in the database.
        opretbrugerTjekEMail = new burger(); //first initialize it before setting any values
        opretbrugerTjekEMail.brugernavn = brugernavn;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 14, 2015 3:18 PM
  • User-1322569703 posted

    error say "The type or namespace name 'burger' could not be found (are you missing a using directive or an assembly reference?)"

    Tuesday, April 14, 2015 3:26 PM
  • User-824764785 posted

    sorry spelling mistake, it should be the table name in which you wanted to add the records

      opretbrugerTjekEMail = new brugere();  //Or the table name which you have added in your dbml file



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 14, 2015 3:30 PM
  • User-1322569703 posted

    ohh okay,

    now its coming "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."

    Tuesday, April 14, 2015 3:49 PM
  • User-824764785 posted

    There must be a colum in sql table with datatype of DateTime set to NOT NULL, you are not passing any value in that column

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2015 2:18 AM