Insert method problems

E

Ed Dror

Hi there,

I have ASP

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Guestbook.aspx.vb"
Inherits="Guestbook" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span style="font-family: Verdana"><strong>Guestbook<br />
<br />
</strong>
<table>
<tr>
<td style="width: 100px">
<span style="font-size: 10pt">
FirstName</span></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<span style="font-size: 10pt">
LastName</span></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Button ID="Button1" runat="server" Text="Submit" /></td>
<td style="width: 100px">
</td>
</tr>
</table>
<br />
</span>
<br />
&nbsp;</div>
</form>
</body>
</html>

And VB

Imports System.Data
Imports System.Data.SqlClient
Public Class ProductInfo
Const conString As String = _
"Server=localhost;Trusted_Connection=true;Database=Northwind"
Public Function MyInsertMethod() As Integer
Dim con As New SqlConnection(conString)
Dim insertString As String = "INSERT INTO Guests (FirstName, LastName)
VALUES ('" & FirstName.Text & "','" & LastName.Text & "')"
Dim cmd As New SqlCommand(insertString, con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Function
End Class
Partial Class Guestbook
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call MyInsertMethod()
End Sub
End Class

I'm getting an error FirstName must be declared
And Call MyInsertMethod must be declared

How do I fixed that and insert a FirstName and LastNAme into database

Thanks,

Ed Dror
 
S

sloan

Public Function MyInsertMethod(lastName as string , firstName as string) As
Integer



dim p as ProductInfo = new ProductInfo()
p.Insert(Me.TextBox1.Text, Me.TextBox2.Text)

...

You also should clean up some stuff. (you may be doing this... but I don't
know for sure)

Don't hard code your connection string, learn how to store it in the
web.config file.

And check this example:
SqlCommand.Prepare Method
http://msdn.microsoft.com/library/d...ClientSqlCommandClassExecuteNonQueryTopic.asp



Dim id As Integer = 20
Dim desc As String = "myFirstRegion"
Dim rConn As SqlConnection = New SqlConnection("Persist Security
Info=False;" & _
"Integrated
Security=SSPI;database=northwind;server=mySQLServer")
rConn.Open()
Dim command As SqlCommand = New SqlCommand("", rConn)

' Create and prepare an SQL statement.
command.CommandText = "insert into Region (RegionID, RegionDescription)
values (@id, @desc)"
command.Parameters.Add("@id", id)
command.Parameters.Add("@desc", desc)
command.Prepare() ' Calling Prepare after having set the Commandtext and
parameters.
command.ExecuteNonQuery()

' Change parameter values and call ExecuteNonQuery.
command.Parameters(0).Value = 21
command.Parameters(1).Value = "mySecondRegion"
command.ExecuteNonQuery()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top