Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2005, 05:27 PM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default errro trying to insert record to sql server

Hi experts. i got this asp.net that supposed to add records to matches table. but when i fill the text boxes
i get the following error.I be happy if some one help me fix this broken code.Thanks

Code:
ERROR: Could not add record, please ensure the fields are correctly filled out
Code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
<script language="VB" runat="server">

    Dim MyConnection As SqlConnection

    Sub Page_Load(Sender As Object, E As EventArgs) 

        MyConnection = New SqlConnection("server=(local);database=teniss2;User Id=web;Password=web;")

        If Not (IsPostBack)
            BindGrid()
        End If
    End Sub

    Sub Addmatches_Click(Sender As Object, E As EventArgs) 

        Dim DS As DataSet
        Dim MyCommand As SqlCommand

        If MATCHNO.Value = "" Or TEAMNO.Value = "" Or PLAYERNO.Value = "" Or WON.Value = ""

          Message.InnerHtml = "ERROR: Null values not allowed "
          Message.Style("color") = "red"
          BindGrid()
        End If

        Dim InsertCmd As String = "insert into matches (MATCHNO, TEAMNO, PLAYERNO ,WON, LOST) values (@LMATCHNO, @LTEAMNO, @LPLAYERNO, @LWON, @LLOST;)"

        MyCommand = New SqlCommand(InsertCmd, MyConnection)

        MyCommand.Parameters.Add(New SqlParameter("@LMATCHNO", SqlDbType.NVarChar, 11))
        MyCommand.Parameters("@LMATCHNO").Value = Server.HtmlEncode(MATCHNO.Value)

        MyCommand.Parameters.Add(New SqlParameter("@LTEAMNO", SqlDbType.NVarChar, 40))
        MyCommand.Parameters("@LTEAMNO").Value = Server.HtmlEncode(TEAMNO.Value)

        MyCommand.Parameters.Add(New SqlParameter("@LPLAYERNO", SqlDbType.NVarChar, 20))
        MyCommand.Parameters("@LPLAYERNO").Value = Server.HtmlEncode(PLAYERNO.Value)

        MyCommand.Parameters.Add(New SqlParameter("@LWON", SqlDbType.NChar, 12))
        MyCommand.Parameters("@LWON").Value = Server.HtmlEncode(WON.Value)

        MyCommand.Parameters.Add(New SqlParameter("@LLOST", SqlDbType.NVarChar, 40))
        MyCommand.Parameters("@LLOST").Value = Server.HtmlEncode(LOST.Value)

     

        MyCommand.Connection.Open()

        Try 
            MyCommand.ExecuteNonQuery()
            Message.InnerHtml = "<b>Record Added</b><br>" & InsertCmd.ToString()

        Catch Exp As SQLException
            If Exp.Number = 2627
                Message.InnerHtml = "ERROR: A record already exists with the same primary key"
            Else
                Message.InnerHtml = "ERROR: Could not add record, please ensure the fields are correctly filled out"
            End If
            Message.Style("color") = "red"

        End Try

        MyCommand.Connection.Close()

        BindGrid()
    End Sub

    Sub BindGrid() 

        Dim MyCommand As SqlDataAdapter = new SqlDataAdapter("select * from matches", MyConnection)

        Dim DS As DataSet = new DataSet()
        MyCommand.Fill(DS, "matches")

        MyDataGrid.DataSource=DS.Tables("matches").DefaultView
        MyDataGrid.DataBind()
    End Sub

</script>

<body style="font: 10pt verdana">

  <form runat="server">

    <h3>Inserting A MATCHES RECORD</h3>

    <table width="95%">
      <tr>
        <td valign="top">

          <ASP:DataGrid id="MyDataGrid" runat="server"
            Width="700"
            BackColor="#ccccff" 
            BorderColor="black"
            ShowFooter="false" 
            CellPadding=3 
            CellSpacing="0"
            Font-Name="Verdana"
            Font-Size="8pt"
            HeaderStyle-BackColor="#aaaadd"
            EnableViewState="false"
          />

        </td>
        <td valign="top">

          <table style="font: 8pt verdana">
            <tr>
              <td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana">Add a New record:</td>
            </tr>
            <tr>
              <td nowrap>MATCHNO: </td>
              <td><input type="text" id="MATCHNO" value="0000" runat="server"></td>
            </tr>
            <tr>
              <td nowrap>TEAMNO: </td>
              <td><input type="text" id="TEAMNO" value="0000" runat="server"></td>
            </tr>  
            <tr nowrap>
              <td>Playerno: </td>
              <td><input type="text" id="PLAYERNO" value="0000" runat="server"></td>
            </tr>
            <tr>
              <td>Won: </td>
              <td><input type="text" id="WON" value="0000" runat="server"></td>
            </tr>
            <tr>
              <td>Lost: </td>
              <td><input type="text" id="LOST" value="0000" runat="server"></td>
            </tr>
           
              <td></td>
              <td style="padding-top:15">
                <input type="submit" OnServerClick="Addmatches_Click" value="Add matches" runat="server">
              </td>
            </tr>
            <tr>
              <td colspan="2" style="padding-top:15" align="center">
                <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/>
              </td>
            </tr>
          </table>

        </td>
      </tr>
    </table>

  </form>

</body>
</html>
 
Old May 20th, 2005, 08:24 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Your sql query is incorrect. You have a ; some it shouldn't be:

@LLOST;)

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
insert multiple record in sql server annie_stwg ASP.NET 2.0 Basics 4 September 19th, 2006 08:56 AM
Insert Multiple Record in SQL Server in Once lauhong28 SQL Server 2000 1 February 24th, 2006 02:43 PM
How to unlock record in SQL Server database DDB001 SQL Server 2000 2 August 2nd, 2004 09:46 AM
Add new record in SQL Server database bhatnagar_shaily Classic ASP Components 3 September 24th, 2003 01:16 AM
Adding a new record to a sql server DB morpheus VB How-To 6 August 6th, 2003 06:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.