Click here to Skip to main content
15,921,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a problem I can not figure out. And I have searched through many websites and I still do not understand why it fails.

I try to enter data into a sql table

Part of Main Page File test2.aspx

<%@ Page Title="test2" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="test2.aspx.vb" Inherits="WebApplication1._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent"> 
    </asp:Content>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <asp:TextBox ID="TextBoxL" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBoxLM" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBoxM" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBoxRM" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBoxR" runat="server"></asp:TextBox>
<asp:Button ID="Buttonismine" runat="server" Text="Click Me" />
    
</asp:Content>
<asp:Content ID="Content1" runat="server" contentplaceholderid="HeadContent">
    </asp:Content>

The code File test2.aspx.vb

VB
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Protected Sub Buttonismine_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Buttonismine_Click

        con = New SqlConnection("Data Source=VNW8;Initial Catalog=Events;Integrated Security=True")
        cmd = New SqlCommand("INSERT INTO World (why,would,you,not,work)VALUES(@R1, @R2, @R3, @R4, @R5)", con)
        cmd.Parameters.AddWithValue("@R1", TextBoxL.Text)
        cmd.Parameters.AddWithValue("@R2", TextBoxLM.Text)
        cmd.Parameters.AddWithValue("@R3", TextBoxM.Text)
        cmd.Parameters.AddWithValue("@R4", TextBoxRM.Text)
        cmd.Parameters.AddWithValue("@R5", TextBoxR.Text)
        cmd.ExecuteNonQuery()
        con.Close()
    End Sub
End Class


And this is how i created the table in SQL Server Managment Studio

SQL
CREATE TABLE [dbo].[World](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[why] [varchar](50) NULL,
	[would] [varchar](50) NULL,
	[you] [varchar](50) NULL,
	[not] [nvarchar](50) NULL,
	[work] [nvarchar](50) NULL,
 CONSTRAINT [PK_Servers] PRIMARY KEY CLUSTERED 
(

Thanks for all the great suggestions. and now I've got some new errors I have to look at.

"handles Buttonismine_click" is Expected
TextBoxL is not declared. It may be inaccessible due to its protection level.
TextBoxLM is not declared. It may be inaccessible due to its protection level.
TextBoxM is not declared. It may be inaccessible due to its protection level.
TextBoxRM is not declared. It may be inaccessible due to its protection level.
TextBoxR is not declared. It may be inaccessible due to its protection level.
Posted
Updated 5-Sep-13 10:21am
v5
Comments
[no name] 5-Sep-13 14:34pm    
It is failing because you are not opening your connection before trying to stuff data into your database.

1 solution

"I still do not understand why it fails."

This isn't much as error reports go - it tells us nothing much about the problem.

But...there is a very big mistake you are making here, which may be the cause of your problem.
VB
cmd = New SqlCommand("INSERT INTO Table1 (row1,row2,row3,row4,row5)VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')", con)

Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
VB
cmd = New SqlCommand("INSERT INTO Table1 (row1,row2,row3,row4,row5)VALUES(@R1, @R2, @R3, @R4, @R5)", con)
cmd.Parameters.AddWithValue("@R1", TextBox1.Text)
cmd.Parameters.AddWithValue("@R1", TextBox2.Text)
cmd.Parameters.AddWithValue("@R1", TextBox3.Text)
cmd.Parameters.AddWithValue("@R1", TextBox4.Text)
cmd.Parameters.AddWithValue("@R1", TextBox5.Text)
There is a good chance that this will cure your problem as well.

And please: for your own sake (and that of the poor sods who have to come after you and clear up the mess during maintenance) stop using default names for things. Even for testing, using "TextBoxN" and "RowN" and "TableN" is silly, lazy and a source of errors later when you can't remember which text box hold what value. So use names that describe what the controls, rows, and tables hold. It doesn't take long, and it really is easier to work with - Intellisense sorts it out for you!
 
Share this answer
 
Comments
prototypen 5-Sep-13 15:08pm    
thanks
I appreciate your advise. This is a test page that does not have anything to do with the main page, it is just to try things out. and get stuff to work before it created in the main Website.
and thanks for the safety tip.
I still have much to learn before i can deploy it.
OriginalGriff 5-Sep-13 15:11pm    
Even for testing, get used to doing things properly - if it becomes second nature it makes life easier. Honest! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900