Insert method problems

Discussion in 'ASP .Net' started by Ed Dror, Jan 31, 2006.

  1. Ed Dror

    Ed Dror Guest

    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
     
    Ed Dror, Jan 31, 2006
    #1
    1. Advertising

  2. Ed Dror

    sloan Guest

    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()




    "Ed Dror" <> wrote in message
    news:...
    > 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
    >
    >
    >
    >
     
    sloan, Feb 1, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cannontrodder
    Replies:
    1
    Views:
    734
    cannontrodder
    Jul 25, 2006
  2. Replies:
    1
    Views:
    454
  3. Replies:
    1
    Views:
    1,788
    Albert Hopkins
    Dec 6, 2008
  4. eagle

    Insert & Insert/Repeat buttons in a detailsview

    eagle, Jul 12, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    1,060
    eagle
    Jul 12, 2007
  5. Feege
    Replies:
    0
    Views:
    468
    Feege
    Dec 20, 2005
Loading...

Share This Page