SQL and VB.NET Problem

tehon3299

Well-known member
Joined
Jan 6, 2003
Messages
155
Location
Liverpool, NY
I have a problem when inserting data into an SQL table from VB.NET...My code is below. All I get in my SQL table is like spaces. Im trying to insert data from a textbox to an SQL table. Very simple but I dont know what I am doing wrong. Any suggestions??

Code:
		<script language="VB" runat="server">
		
			Sub SubmitBtn_Click(sender As Object, e As EventArgs)
				Dim MyCommand As SqlCommand
				Dim MyConnection As SqlConnection
				Dim MySQLDataAdapter as SqlDataAdapter
				
				MyConnection = New SqlConnection("server=(local);database=checking;Trusted_Connection=yes")
								
				Dim SessionCmd As String = "INSERT INTO tmTransactions (Description) VALUES (" & txtDesc.Text & ")"
				MyCommand = New SqlCommand(SessionCmd, MyConnection)

				MyCommand.Connection.Open()
					
					Try 
						MyCommand.ExecuteNonQuery()
					Catch Exp As SQLException
						If Exp.Number = 2627
			                
						Else
			                
						End If
					End Try
					
				MyCommand.Connection.Close()
			End Sub

			Sub Page_Load(Sender As Object, E As EventArgs)
				txtDesc.Text = ""
				txtDate.Text = ""
			End Sub
			
		</script>

Thanks
 
OK...I did that and my code is below. Still is just inserting blank spaces in the SQL table. Anything else you can see??

Code:
		<script language="VB" runat="server">
		
			Sub SubmitBtn_Click(sender As Object, e As EventArgs)
				Dim MyCommand As New SqlCommand
				Dim MyConnection As New SqlConnection
				Dim MySQLDataAdapter as New SqlDataAdapter
				
				MyConnection = New SqlConnection("server=(local);database=checking;Trusted_Connection=yes")
								
				Dim SessionCmd As String = "INSERT INTO tmTransactions (Description) VALUES (" & txtDesc.Text & ")"
				MyCommand = New SqlCommand(SessionCmd, MyConnection)

				MyCommand.Connection.Open()
					
					Try
						MyCommand.ExecuteNonQuery()
					Catch Exp As SQLException
						If Exp.Number = 2627
			                
						Else
			                
						End If
					End Try
					
				MyCommand.Connection.Close()
			End Sub

			Sub Page_Load(Sender As Object, E As EventArgs)
				txtDesc.Text = ""
				txtDate.Text = ""
			End Sub
			
		</script>

Thanks
 
Well if its inserting blank spaces then perhaps the problem is with txtDesc.Text? Have you tried hard coding a description?
 
Good Question. I tried to hard code something and it inserts it fine into the table. Could there be something wrong with the code in my body? Take a look...
Code:
	<body bgcolor="#000000">
			<form runat="server">
			<font face="helvetica" size="3" color="#aa9933">
			<asp:RadioButton Text="Deposit" ID=RadioDeposit GroupName="Radio1" runat="server"></asp:RadioButton>
			<br><asp:RadioButton Text="Withdrawl" ID=RadioWithdrawl GroupName="Radio1" runat="server"></asp:RadioButton>
			<br><br>Date:<br><asp:TextBox ID=txtDate name="@Date" runat="server"></asp:TextBox>
			<br><br>Description:<br><asp:TextBox ID=txtDesc runat="server"></asp:TextBox>
			<br><br>Amount:<br><asp:TextBox ID=txtAmount runat="server"></asp:TextBox>
			<br><br><asp:Button ID=SubmitBtn name="SubmitBtn" Text="Submit" OnClick="SubmitBtn_Click" runat="server"></asp:Button>
			</form>
     </body>

Thanks
 
OK...I figured it out...In the Sub Page Load function I am clearing the textboxes...I dont know why it screws it up but when I comment it out it inserts it into the table fine. It stills screws up tho when I have a space in the textbox. How can I fix this??

Thanks everyone
 
Back
Top