简体   繁体   English

在html文本框中输入数据并在数据库中显示

[英]Enter data in html textbox and show it in database

I have created two tables: Artist and Song. 我创建了两个表:Artist和Song。 Artist table has fields Artistid and ArtistName and Song table has SongID, Lyrics, Description and Artistid as the secondary key. Artist表具有字段Artistid和ArtistName,Song表具有SongID,Lyrics,Description和Artistid作为辅助键。 I have a web form to add new artist containing a textbox and submit button. 我有一个Web表单来添加包含文本框和提交按钮的新艺术家。

The code is as below : 代码如下:

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

My requirement is that I will enter some name in textbox and click on submit , the same name should be shown in artist table in the database . 我的要求是,我将在文本框中输入一些名称,然后单击“提交”,该名称应在数据库的艺术家表中显示。 I have also created a namespace using Mygenerations with the artist and song table . 我还使用Mygenerations和艺术家和歌曲表创建了一个命名空间。 I have written the foll code but it does not show the data in database .it does not work properly . 我已经写了以下代码,但是它没有显示数据库中的数据。它无法正常工作。 Please help. 请帮忙。 The code behind file is as below : 文件后面的代码如下:

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

Also I am getting an error for the line that has textArtist.Value . 另外,对于具有textArtist.Value的行,我也遇到了错误。 The error is textArtist does not exist in current context. 错误是textArtist在当前上下文中不存在。 Please tell me where I'm going wrong. 请告诉我我要去哪里了。

Can be more reasons but for now you have used txtArtist as the Id. 可能还有更多原因,但是到目前为止,您已经使用txtArtist作为ID。 So use this 所以用这个

  string s_artistname = txtArtist.Value;

and

  oArtist.ArtistName = txtArtist.Value;

如果要在后台代码中访问它,请使用runat=server

<input type="text" name ="textArtist" id ="txtartist"  runat="server" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM