[英]Enter data in html textbox and show it in database
我创建了两个表:Artist和Song。 Artist表具有字段Artistid和ArtistName,Song表具有SongID,Lyrics,Description和Artistid作为辅助键。 我有一个Web表单来添加包含文本框和提交按钮的新艺术家。
代码如下:
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" method ="post">
<div style="width: 960px; color: black; border: 2px solid black; text-align:"center">
<table>
<tr>
<td> <label>Artist: </label>
<input type="text" name ="textArtist" id ="txtartist" value = "" />
</td>
</tr>
<tr>
<td>
<p align="center" >
<input type = "button" id= "btnArtist" value ="submit" />
</p>
</td>
</tr>
</table>
</div>
</form>
</body>
我的要求是,我将在文本框中输入一些名称,然后单击“提交”,该名称应在数据库的艺术家表中显示。 我还使用Mygenerations和艺术家和歌曲表创建了一个命名空间。 我已经写了以下代码,但是它没有显示数据库中的数据。它无法正常工作。 请帮忙。 文件后面的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NCI.EasyObjects ;
using TestSong ;
public partial class _Default : System.Web.UI.Page
{
string strcon = "Company";
protected void Page_Load(object sender, EventArgs e)
{
}
void btnartist_onclick(object sender, EventArgs e)
{
string s_artistname = textArtist.Value;
Artist oArtist = new Artist(strcon);
oArtist.Where.ArtistName.Value = s_artistname;
if (!oArtist.Query.Load())
{
oArtist.AddNew();
oArtist.ArtistName = textArtist.Value;
oArtist.Save();
}
}
}
另外,对于具有textArtist.Value
的行,我也遇到了错误。 错误是textArtist
在当前上下文中不存在。 请告诉我我要去哪里了。
可能还有更多原因,但是到目前为止,您已经使用txtArtist作为ID。 所以用这个
string s_artistname = txtArtist.Value;
和
oArtist.ArtistName = txtArtist.Value;
如果要在后台代码中访问它,请使用runat=server
<input type="text" name ="textArtist" id ="txtartist" runat="server" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.