Can anyone help me with why my insert statement won't work? I have tried countless tutorials online, but I cannot figure out what I am doing wrong. Can anyone point me int the right direction here? Here is my form:

form:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="form.aspx.cs" Inherits="_form" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

   
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Add Character</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Add</h1>
    Identity:
    <asp:TextBox ID="char_identity" runat="server" /><br />
    Name:
    <asp:TextBox ID="name" runat="server" /><br /><br />
    <asp:Button ID="add_char" Text="Add Character" runat="server" 
            onclick="add_char_Click" /><br /><br />
    <asp:Label ID="dbErrorMessage" ForeColor="Red" runat="server" />
    </div>
    </form>
</body>
</html>

form (code-behind):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class _form : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 protected void add_char_Click(object sender, EventArgs e)
    {
     SqlConnection conn;
     SqlCommand comm;
     string connectionString = ConfigurationManager.ConnectionStrings["smrpg"].ConnectionString;
     conn = new SqlConnection(connectionString);
     comm = new SqlCommand("INSERT INTO scouts (charID, char_identity, name) VALUES (@charID, @char_identity, @name)", conn);
     comm.Parameters.Add("@charID", System.Data.SqlDbType.Int);
     comm.Parameters["@charID"].Value = 10;
     comm.Parameters.Add("@char_identity", System.Data.SqlDbType.NVarChar, 50);
     comm.Parameters["@char_identity"].Value = char_identity.Text;
     comm.Parameters.Add("@name", System.Data.SqlDbType.NVarChar, 50);
     comm.Parameters["@name"].Value = name.Text;
     try
     {
         conn.Open();
         comm.ExecuteNonQuery();
         Response.Redirect("repeater.aspx");
     }
     catch
     {
         dbErrorMessage.Text = "Error submitting the character! Please try again later, and/or change the entered data!";
     }
     finally
     {
         conn.Close();
     }
 }
    }

I have three fields:

charID, int, allow null unchecked, primary key, is identity yes
char_identity, nvarchar(50), allow null unchecked
name, nvarchar(50), allow null unchecked

What have I done wrong? I am connecting to the database and can display the records in the table, I just can't insert new records from a form for some reason. What am I doing wrong? It only displays my error message when I try to submit the form.

Recommended Answers

All 2 Replies

Remove charid both from insert statement.

Thank you so much! Finally!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.