Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • BrianAhle 4 posts 34 karma points
    Dec 14, 2011 @ 20:25
    BrianAhle
    0

    Simple C# SQL code help :(

    mjellow!

    I'm doing some very simple C# code in Visual Studio, with a little SQL.
    It is pretty simple;
    I've got some textboxes, and want the entered to get saved in my SQL database.
    Googled all day long, and cant seem to find a solution fitting me -.-
    The code I go so far, which I cant understand why not will work:



    status.aspx:

    <form id="form1" runat="server>

      <asp:Label ID="Label1" runat="server" Text="EmployeeID:"></asp:Label>
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
           <asp:Label ID="Label2" runat="server" Text="FirstName:"></asp:Label>
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
           <asp:Label ID="Label3" runat="server" Text="LastName:"></asp:Label>
       <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
           <asp:Label ID="Label4" runat="server" Text="Address:"></asp:Label>
       <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
           <asp:Label ID="Label5" runat="server" Text="Mobile:"></asp:Label>
       <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />

    </form>




    status.aspx.cs:

           string connection;
           connection = System.Configuration.ConfigurationManager.ConnectionStrings["mycon"].ConnectionString.ToString();
           con = new SqlConnection(connection);

       protected void btnNew_Click(object sender, EventArgs e)
       {

              SqlCommand cmd = new SqlCommand();
              string query = "INSERT INTO EMPLOYEE VALUES(@EmpID,@FirstName,@LastName,@Address,@Mobile";

                  cmd.CommandText = query;

                  cmd.Parameters.AddWithValue("@EmpID", TextBox1);
                  cmd.Parameters.AddWithValue("@FirstName", TextBox2.Text );
                  cmd.Parameters.AddWithValue("@LastName",TextBox3.Text );
                  cmd.Parameters.AddWithValue("@Address",TextBox4.Text );
                  cmd.Parameters.AddWithValue("@Mobile",TextBox5.Text );

                  cmd.Connection = con;
                  con.Open();
                  cmd.ExecuteNonQuery();
                  con.Close();

           } 



    It gives me an error at "cmd.ExecuteNonQuery();"

    Fail description:

    No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.ArgumentException: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.

     


    but why?! Hope someone can help me, before im gonna loose all my hair!

  • elspiko 133 posts 302 karma points
    Dec 14, 2011 @ 20:29
    elspiko
    0

    The issue lies with the line cmd.Parameters.AddWithValue("@EmpID", TextBox1); It should be

    cmd.Parameters.AddWithValue("@EmpID", TextBox1.Text);

  • BrianAhle 4 posts 34 karma points
    Dec 14, 2011 @ 20:33
    BrianAhle
    0

    Oh my f..... god.... I did not put it in there with purpose, because it was my primary key, and didnt think it should have a text....

    - Just spended 5 hours on this, and u saw it for like 6 min... 
    U must be.... my hero - of some kind :D

    FANX!


    EDIT:
    Is there an easy trick, to make the button refresh the page, so I can see the saved sql right away in my grid? :)

Please Sign in or register to post replies

Write your reply to:

Draft