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 outCode:<%@ 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><font face="Verdana">Inserting A MATCHES RECORD</font></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>




Reply With Quote