Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    IOB
    IOB is offline
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ADO.net - Inserting record problem

    When I use the proper SQL statements within ADO to insert a record into my test database using parameters (values from textboxes), nothing is passed at all and everything is null. However, if I pass a hard value, the table does recieve the data. I looked over my code a bunch of times but can't find one thing wrong as to why this is happening. Any help?

    Code:
    <%@ Import Namespace = "MySql.Data.MySqlClient"%> 
    <%@ Page Language="C#" Debug="true" %>
    <Script Runat="server">
    
    	
    	
    	protected void submitClick(object s, EventArgs e){
    	
    	MySqlConnection conn;
    	MySqlCommand comm;
    
    	conn=new MySqlConnection("Server=localhost;user id=testuser;pwd=password;database=test");
    		
    	
    	comm = new MySqlCommand(
    	"INSERT INTO Dev07 (cliID)" +
    	"VALUES (@cliID)", conn);
    
    	comm.Parameters.Add("@cliID",clientTextBox.Text);
    	conn.Open();
    	comm.ExecuteNonQuery();
    	conn.Close();
    	
    } 
    
    
       </script>
    <html>
    <body>
    <form Runat="Server">
    
    <p>
    Account Number
    <asp:TextBox
    	Id="clientTextBox"
    	MaxLength="7"
    	Runat="Server" />
    <p>
    <asp:Button
    	Id="submit"
    	OnClick="submitClick"
    	Text="submit"
    	runat="server" />
    
    </form>
    </body>
    </html>

  • #2
    Regular Coder Freon22's Avatar
    Join Date
    May 2005
    Location
    USA
    Posts
    287
    Thanks
    3
    Thanked 5 Times in 5 Posts
    I code VB not C# but I believe the Parameters are the same in both.
    Code:
    	comm = new MySqlCommand(
    	"INSERT INTO Dev07 (cliID)" +
    	"VALUES (@cliID)", conn);
    
    	comm.Parameters.Add("@cliID",clientTextBox.Text);
    	conn.Open();
    	comm.ExecuteNonQuery();
    	conn.Close();
    Try this AddWithValues.
    Code:
    comm.Parameters.AddWithValue("@cliID", clientTextBox.Text)

  • #3
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    I had a problem with .NET 1.1 and parameters like this as well. Never did solve it.
    Using 2.0, no problem with AddWithValue. So now I just use that.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •