主题:帮我看看
帮我看看这个代码
我新学ASP.NET
添加数据的时候
出现 (发生错误,没有正常插入记录)
应该不出来我写都是书上一样的但是发生错误了
在线=高手的答案!
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
Sub Enter_Click(Sender As Object, E As EventArgs)
'建立Connection对象
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.Mappath("wwwlink.mdb"))
'建立Command对象
Dim strA,strB,strSql As String
strA="Insert Into link(sitename,URL,grade,submit_date"
strB=" Values('" & sitename.text & "','" & URL.text & "'," & cInt(grade.SelectedItem.Text) & ",#" & Now() & "#"
If Trim(intro.Text)<>"" Then '如果网站简介不为空,就执行
strA=strA & ",intro"
strB=strB & ",'" & intro.Text & "'"
End If
strSql=strA & ")" & strB & ")"
Dim cmd As New OleDbCommand(strSql, conn)
Dim Exp As Exception
Try
'执行操作,插入记录
conn.open()
cmd.ExecuteNonQuery()
conn.close()
Response.Redirect("index.aspx") '正常添加后,返回首页
Catch Exp
message.Text="发生错误,没有正常插入记录"
End Try
End Sub
</script>
<html>
<body>
<h4 align="center">插入记录</h4>
<center>
<form runat="server">
<table border="1" width="80%" cellpading="2" cellspacing="0">
<tr>
<td>网站名称:</td>
<td><asp:textbox id="sitename" runat="server"/>
<asp:RequiredFieldValidator id="Require1" ControlToValidate="sitename" ErrorMessage="必须输入名称" Display="Static" runat="server" /></td>
</tr>
<tr>
<td>网站地址:</td>
<td>http://<asp:textbox id="URL" columns="40" runat="server"/>
<asp:RequiredFieldValidator id="Require2" ControlToValidate="URL" ErrorMessage="必须输入地址" Display="Static" runat="server" /></td>
</tr>
<tr>
<td>网站简介:</td>
<td><asp:textbox id="intro" textmode="multiline" columns="40" rows="4" runat="server"/></td>
</tr>
<tr>
<td>网站评分:</td>
<td>
<asp:dropdownlist id="grade" runat="server">
<asp:listitem>1</asp:listitem>
<asp:listitem>2</asp:listitem>
<asp:listitem selected="True">3</asp:listitem>
<asp:listitem>4</asp:listitem>
<asp:listitem>5</asp:listitem>
</asp:dropdownlist >
</td>
</tr>
<tr>
<td></td>
<td><asp:button id="Enter" text=" 提 交 " onClick="Enter_Click" runat="server"/></td>
</tr>
</table>
<asp:Label id="message" runat="server"/>
</form>
<a href="index.aspx">取消添加,返回首页</a>
</center>
</body>
</html>
我新学ASP.NET
添加数据的时候
出现 (发生错误,没有正常插入记录)
应该不出来我写都是书上一样的但是发生错误了
在线=高手的答案!
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
Sub Enter_Click(Sender As Object, E As EventArgs)
'建立Connection对象
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.Mappath("wwwlink.mdb"))
'建立Command对象
Dim strA,strB,strSql As String
strA="Insert Into link(sitename,URL,grade,submit_date"
strB=" Values('" & sitename.text & "','" & URL.text & "'," & cInt(grade.SelectedItem.Text) & ",#" & Now() & "#"
If Trim(intro.Text)<>"" Then '如果网站简介不为空,就执行
strA=strA & ",intro"
strB=strB & ",'" & intro.Text & "'"
End If
strSql=strA & ")" & strB & ")"
Dim cmd As New OleDbCommand(strSql, conn)
Dim Exp As Exception
Try
'执行操作,插入记录
conn.open()
cmd.ExecuteNonQuery()
conn.close()
Response.Redirect("index.aspx") '正常添加后,返回首页
Catch Exp
message.Text="发生错误,没有正常插入记录"
End Try
End Sub
</script>
<html>
<body>
<h4 align="center">插入记录</h4>
<center>
<form runat="server">
<table border="1" width="80%" cellpading="2" cellspacing="0">
<tr>
<td>网站名称:</td>
<td><asp:textbox id="sitename" runat="server"/>
<asp:RequiredFieldValidator id="Require1" ControlToValidate="sitename" ErrorMessage="必须输入名称" Display="Static" runat="server" /></td>
</tr>
<tr>
<td>网站地址:</td>
<td>http://<asp:textbox id="URL" columns="40" runat="server"/>
<asp:RequiredFieldValidator id="Require2" ControlToValidate="URL" ErrorMessage="必须输入地址" Display="Static" runat="server" /></td>
</tr>
<tr>
<td>网站简介:</td>
<td><asp:textbox id="intro" textmode="multiline" columns="40" rows="4" runat="server"/></td>
</tr>
<tr>
<td>网站评分:</td>
<td>
<asp:dropdownlist id="grade" runat="server">
<asp:listitem>1</asp:listitem>
<asp:listitem>2</asp:listitem>
<asp:listitem selected="True">3</asp:listitem>
<asp:listitem>4</asp:listitem>
<asp:listitem>5</asp:listitem>
</asp:dropdownlist >
</td>
</tr>
<tr>
<td></td>
<td><asp:button id="Enter" text=" 提 交 " onClick="Enter_Click" runat="server"/></td>
</tr>
</table>
<asp:Label id="message" runat="server"/>
</form>
<a href="index.aspx">取消添加,返回首页</a>
</center>
</body>
</html>