Help with insert statement

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 21
Reputation: twilitegxa is an unknown quantity at this point 
Solved Threads: 0
twilitegxa twilitegxa is offline Offline
Newbie Poster

Help with insert statement

 
0
  #1
23 Days Ago
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:
  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):
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 217
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 31
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training
 
0
  #2
23 Days Ago
Remove charid both from insert statement.
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 21
Reputation: twilitegxa is an unknown quantity at this point 
Solved Threads: 0
twilitegxa twilitegxa is offline Offline
Newbie Poster
 
0
  #3
23 Days Ago
Thank you so much! Finally!
Last edited by twilitegxa; 23 Days Ago at 5:05 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC