944,117 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 513
  • ASP.NET RSS
Nov 4th, 2009
0

Help with insert statement

Expand Post »
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:
ASP.NET Syntax (Toggle Plain Text)
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="form.aspx.cs" Inherits="_form" %>
  2. <%@ Import Namespace="System.Data.SqlClient" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <script runat="server">
  6.  
  7.  
  8. </script>
  9.  
  10. <html xmlns="http://www.w3.org/1999/xhtml">
  11. <head runat="server">
  12. <title>Add Character</title>
  13. </head>
  14. <body>
  15. <form id="form1" runat="server">
  16. <div>
  17. <h1>Add</h1>
  18. Identity:
  19. <asp:TextBox ID="char_identity" runat="server" /><br />
  20. Name:
  21. <asp:TextBox ID="name" runat="server" /><br /><br />
  22. <asp:Button ID="add_char" Text="Add Character" runat="server"
  23. onclick="add_char_Click" /><br /><br />
  24. <asp:Label ID="dbErrorMessage" ForeColor="Red" runat="server" />
  25. </div>
  26. </form>
  27. </body>
  28. </html>

form (code-behind):
ASP.NET Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Configuration;
  9.  
  10. public partial class _form : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. protected void add_char_Click(object sender, EventArgs e)
  16. {
  17. SqlConnection conn;
  18. SqlCommand comm;
  19. string connectionString = ConfigurationManager.ConnectionStrings["smrpg"].ConnectionString;
  20. conn = new SqlConnection(connectionString);
  21. comm = new SqlCommand("INSERT INTO scouts (charID, char_identity, name) VALUES (@charID, @char_identity, @name)", conn);
  22. comm.Parameters.Add("@charID", System.Data.SqlDbType.Int);
  23. comm.Parameters["@charID"].Value = 10;
  24. comm.Parameters.Add("@char_identity", System.Data.SqlDbType.NVarChar, 50);
  25. comm.Parameters["@char_identity"].Value = char_identity.Text;
  26. comm.Parameters.Add("@name", System.Data.SqlDbType.NVarChar, 50);
  27. comm.Parameters["@name"].Value = name.Text;
  28. try
  29. {
  30. conn.Open();
  31. comm.ExecuteNonQuery();
  32. Response.Redirect("repeater.aspx");
  33. }
  34. catch
  35. {
  36. dbErrorMessage.Text = "Error submitting the character! Please try again later, and/or change the entered data!";
  37. }
  38. finally
  39. {
  40. conn.Close();
  41. }
  42. }
  43. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
twilitegxa is offline Offline
23 posts
since Jun 2008
Nov 4th, 2009
0
Re: Help with insert statement
Remove charid both from insert statement.
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009
Nov 4th, 2009
0
Re: Help with insert statement
Thank you so much! Finally!
Last edited by twilitegxa; Nov 4th, 2009 at 5:05 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
twilitegxa is offline Offline
23 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Update.......
Next Thread in ASP.NET Forum Timeline: To pass login information in asp.net c# crystal report





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC