locked
Insert into Access RRS feed

  • Question

  • User-712928679 posted

     Hi all,

    I'm having some problems insert information into a Access database. I'm not a coder, just a network admin so excuse me if this is basic knowledge :)
    Here's the .cs code:

     

    1     public static string GetConnString()
    2        {
    3            return ConfigurationManager.ConnectionStrings["Lab3String"].ConnectionString;
    4        }
    5    
    6        protected void Page_Load(object sender, EventArgs e)
    7        {
    8           
    9        }
    10   
    11       protected void btnSkicka_Click(object sender, EventArgs e)
    12       {
    13           
    14           string ConnString = GetConnString();
    15           string SqlString = "Insert Into Information (Fnamn, Enamn, Adress1, Adress2, Telefon, Mobil, Epost, Nyhetsbrev) Values (@Fnamn, @Enamn, @Adress1, @Adress2, @Telefon, @Mobil, @Epost, @Nyhetsbrev)";
    16   
    17           using (OleDbConnection conn = new OleDbConnection(ConnString))
    18           {
    19               using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
    20               {
    21                   
    22                   cmd.CommandType = CommandType.Text;
    23                   cmd.Parameters.AddWithValue("Fnamn", txtFnamn.Text);
    24                   cmd.Parameters.AddWithValue("Enman", txtEnamn.Text);
    25                   cmd.Parameters.AddWithValue("Adress1", txtAdress1.Text);
    26                   cmd.Parameters.AddWithValue("Adress2", txtAdress2.Text);
    27                   cmd.Parameters.AddWithValue("Telefon", txtTelefon.Text);
    28                   cmd.Parameters.AddWithValue("Mobil", txtMobil.Text);
    29                   cmd.Parameters.AddWithValue("Epost", txtEpost.Text);
    30                   cmd.Parameters.AddWithValue("Nyhetsbrev", rblNyhetsbrev.Text);
    31                   conn.Open();
    32                   cmd.ExecuteNonQuery();
    33   
    34               }
    35   
    36           }
    37       }
    
      Can anyone see what the problem is? When I run the code, noting happens...
    Wednesday, April 15, 2009 6:16 AM

Answers

  • User1191518856 posted

    Overflow typically means that you are trying to insert a value that it larger than what's been specified for the column in the database. If I read this correctly, the error is related to the "Nyhetsbrev" column. What is rblNyhetsbrev.Text? Is it a RadioButtonList (check the aspx)? What is the type of "Nyhetsbrev" column in the database? These two must match.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2009 7:29 AM
  • User-712928679 posted

     I changed the memofield in the database to a Text field and now it's working :)

    Thanks for all your help.

    Translation:

    Tack för all din hjälp :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2009 7:50 AM

All replies

  • User1191518856 posted

    You need to make sure the btnSkicka_Click method is wired to a button. Look in the aspx file for the definition of the button. There you should see an OnClick attribute which should read OnClick="btnSkicka_Click". Unless you see this, the button will not trigger any action.

    Lycka till!

    Wednesday, April 15, 2009 7:09 AM
  • User-712928679 posted

    Thanks for your help. I look into the Default.aspx file and there wasn't any OnClick function so I added it. Now when I run the code I get the following error:

    Exception Details: System.Data.OleDb.OleDbException: Overflow

     

    Line 47:                 cmd.Parameters.AddWithValue("Nyhetsbrev", rblNyhetsbrev.Text);
    Line 48:                 conn.Open();
    Line 49:                 cmd.ExecuteNonQuery();
    Line 50: 
    Line 51:             }
     

    Wednesday, April 15, 2009 7:24 AM
  • User1191518856 posted

    Overflow typically means that you are trying to insert a value that it larger than what's been specified for the column in the database. If I read this correctly, the error is related to the "Nyhetsbrev" column. What is rblNyhetsbrev.Text? Is it a RadioButtonList (check the aspx)? What is the type of "Nyhetsbrev" column in the database? These two must match.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2009 7:29 AM
  • User-712928679 posted

     In the .aspx code the rblNyhetsbrev is a radiobuttonlist and in the acess database it's a memo field.

    .aspx:
     

    1    <asp:RadioButtonList ID="rblNyhetsbrev" runat="server">
    2             <asp:ListItem>Ja</asp:ListItem>
    3             <asp:ListItem>Nej</asp:ListItem>
    4        </asp:RadioButtonList>
    
     
    Wednesday, April 15, 2009 7:37 AM
  • User-712928679 posted

     I changed the memofield in the database to a Text field and now it's working :)

    Thanks for all your help.

    Translation:

    Tack för all din hjälp :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2009 7:50 AM