Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: trouble with code and inserting data into DB


Message #1 by "dave" <dougwood@a...> on Tue, 22 Oct 2002 21:46:50
HI,
I am working on an exercise in Beg ASP.NET with C# (Chapter 15) in which I 
am trying to create a form where the user enters a name and password and 
those names get inserted into a my Access .mdb. Seems pretty simple but 
something is wrong.
I have simplified the code a bit just to make it easier. I know exactly 
where the problem is but can't seem to get the correct syntax to make it 
work.
Here is my code in which a user fills out:

Please enter your email:
<asp:textbox id="email" runat="server" text="enter valid addy"/>

Please enter your password:
<asp:textbox id="password" runat="server" text="enter password"/>

<asp:button id="validate" OnClick="Validate_Login" Text="Enter" 
runat="server"/>
		
<asp:label id="result" runat="server"/>


and then this is the code that should enter the info into my database:


public class MyLoginClass : UserControl
{
	public TextBox password;
	public TextBox email;
	public Label result;
	
	
		public void Validate_Login(object sender, EventArgs e)
		{
		string PasswordValue, EmailValue;
		EmailValue = email.Text;
		PasswordValue = password.Text;
			
		OleDbConnection objConnection = null;
    		OleDbCommand objCmd = null;

    		string strConnection, strSQL;
    		strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
    		strConnection += @"Data Source=C:\BegASPNET\login.mdb"; 
	
    		objConnection = new OleDbConnection(strConnection);
    		objConnection.Open();

    		strSQL = "INSERT INTO Login (FirstName, LastName) " 
+ "VALUES(EmailValue, PasswordValue)";

    		objCmd = new OleDbCommand(strSQL, objConnection);

    	        objCmd.ExecuteNonQuery();
			
		result.Text = "your email address " + EmailValue + "and 
your password " + PasswordValue + " have been stored in the database";
objConnection.Close();
		}
	} 
			
Whern I run this I get this error:

System.Data.OleDb.OleDbException: No value given for one or more required 
parameters.

Now I know that the problem resides in my INSERT statement where the 
Values are input. If I replace VALUES(EmailValue, PasswordValue) with 
actual strings , ie: Values('Doug@a...' and 'mypassword'), everthing 
works fine and the DB has created a new row with this info. But this is 
obviuosly not what I want!!

What is it about passing the parameters EmailValue and PasswordValue into 
my INSERT statement that is wrong?????

I appreciate any help, this is really frustrating!!!!
Thanks!!!!
Message #2 by "Jack" <jdunstan7@h...> on Tue, 22 Oct 2002 21:14:18 -0400
Try adding the single quotes, like below:

    strSQL = "INSERT INTO Login (FirstName, LastName) " 
+ "VALUES('" + EmailValue + "' ,'" + PasswordValue + "')";

hth
Jack

  ----- Original Message ----- 
  From: dave 
  To: aspx_beginners 
  Sent: Tuesday, October 22, 2002 9:46 PM
  Subject: [aspx_beginners] trouble with code and inserting data into DB


  HI,
  I am working on an exercise in Beg ASP.NET with C# (Chapter 15) in which I 
  am trying to create a form where the user enters a name and password and 
  those names get inserted into a my Access .mdb. Seems pretty simple but 
  something is wrong.
  I have simplified the code a bit just to make it easier. I know exactly 
  where the problem is but can't seem to get the correct syntax to make it 
  work.
  Here is my code in which a user fills out:

  Please enter your email:
  <asp:textbox id="email" runat="server" text="enter valid addy"/>

  Please enter your password:
  <asp:textbox id="password" runat="server" text="enter password"/>

  <asp:button id="validate" OnClick="Validate_Login" Text="Enter" 
  runat="server"/>

  <asp:label id="result" runat="server"/>


  and then this is the code that should enter the info into my database:


  public class MyLoginClass : UserControl
  {
  public TextBox password;
  public TextBox email;
  public Label result;


  public void Validate_Login(object sender, EventArgs e)
  {
  string PasswordValue, EmailValue;
  EmailValue = email.Text;
  PasswordValue = password.Text;

  OleDbConnection objConnection = null;
      OleDbCommand objCmd = null;

      string strConnection, strSQL;
      strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
      strConnection += @"Data Source=C:\BegASPNET\login.mdb"; 

      objConnection = new OleDbConnection(strConnection);
      objConnection.Open();

      strSQL = "INSERT INTO Login (FirstName, LastName) " 
  + "VALUES(EmailValue, PasswordValue)";

      objCmd = new OleDbCommand(strSQL, objConnection);

              objCmd.ExecuteNonQuery();

  result.Text = "your email address " + EmailValue + "and 
  your password " + PasswordValue + " have been stored in the database";
  objConnection.Close();
  }
  } 

  Whern I run this I get this error:

  System.Data.OleDb.OleDbException: No value given for one or more required 
  parameters.

  Now I know that the problem resides in my INSERT statement where the 
  Values are input. If I replace VALUES(EmailValue, PasswordValue) with 
  actual strings , ie: Values('Doug@a...' and 'mypassword'), everthing 
  works fine and the DB has created a new row with this info. But this is 
  obviuosly not what I want!!

  What is it about passing the parameters EmailValue and PasswordValue into 
  my INSERT statement that is wrong?????

  I appreciate any help, this is really frustrating!!!!
  Thanks!!!!
  ---
  Beginning ASP.NET Databases using VB.NET
  http://www.wrox.com/ACON11.asp?ISBN=1861006195

  Beginning ASP.NET Databases using C#
  http://www.wrox.com/ACON11.asp?ISBN=1861007418

  These books look at how we can create data-centric ASP.NET 
  applications. Requiring some basic knowledge of ASP.NET, 
  Access and SQL the authors guide you through the process 
  of connecting and consuming information in a variety of 
  ways. They are packed full of excellent illustrative code 
  examples, demonstrating important fundamental principles.


  Return to Index