简体   繁体   中英

Enter data in html textbox and show it in database

I have created two tables: Artist and Song. Artist table has fields Artistid and ArtistName and Song table has SongID, Lyrics, Description and Artistid as the secondary key. I have a web form to add new artist containing a textbox and submit button.

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 . 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 . The error is textArtist does not exist in current context. Please tell me where I'm going wrong.

Can be more reasons but for now you have used txtArtist as the 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" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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