Access value of HTML Input TextBox in code behind using C# and VB.Net

Last Reply one year ago By dharmendr

Posted one year ago

Hi

I have an issue in inserting input text value into database table.

Issue is if I add runat=server in input text control data could not populate from my Smartcard device.

If I remove runat=server, I could not save, Error is not member..

Here is my code

<div class="row">
    <div class="col-lg-3">
        <asp:Label ID="Label1" runat="server" Text="NRIC No" CssClass="txtsize"></asp:Label>
        <input id="NRICNo" type="text" name="NRICNo" class="form-control cbo txtsize" runat="server" />
    </div>
</div>
<div class="row">
    <div class="col-lg-3">
        <asp:Label ID="Label2" runat="server" Text="Gender" CssClass="txtsize"></asp:Label>
        <input id="Gender" type="text" name="Gender" class="form-control cbo txtsize" runat="server" />
    </div>
</div>
<div class="row">
    <div class="col-lg-3">
        <asp:Label ID="Label3" runat="server" Text="Date Birth" CssClass="txtsize"></asp:Label>
        <input id="DOB" type="text" name="DOB" class="form-control cbo txtsize" runat="server" />
    </div>
</div>
<div class="row">
    <div class="col-lg-3">
        <asp:Label ID="Label4" runat="server" Text="Birth Place" CssClass="txtsize"></asp:Label>
        <input id="BirthPlace" type="text" name="BirthPlace" class="form-control cbo txtsize"
            runat="server" />
    </div>
</div>
<div class="row">
    <div class="col-lg-3">
        <asp:Label ID="Label5" runat="server" Text="Race" CssClass="txtsize"></asp:Label>
        <input id="Race" type="text" name="race" class="form-control cbo txtsize" runat="server" />
    </div>
</div>
<div class="row">
    <div class="col-lg-11">
        <asp:Label ID="Label6" runat="server" Text="Photo" CssClass="txtsize"></asp:Label>
        <input id="Photo" type="text" name="BirthPlace" class="form-control txtsize" runat="server" />
    </div>
</div>
<div class="row">
    <div class="col-lg-5">
        <br />
        <asp:Button ID="btnSave" runat="server" Text="Submit" CssClass="btn btn-primary" />
    </div>
</div>

 

Imports dbConnection
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager
Imports System.Configuration

Partial Class Help_MyKad
    Inherits System.Web.UI.Page
    Dim cmd As New SqlCommand
    Dim data As SqlDataReader
    Dim i As Integer
    Dim errmsg As String

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then

        End If
    End Sub

    Private Sub InsertNRICData()
        cmd = New SqlCommand
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "INSERT INTO Temp_MyKad ( Name, NRICNo, Gender, DOB, BirthPlace, Race, ImageType, ImageData)" &
                          " VALUES (@Name,@NRICNo,@Gender,@DOB,@BirthPlace,@Race,@ImageType,@ImageData)"
        Try
            conn.Open()
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name.Value
            cmd.Parameters.Add("@NRICNo", SqlDbType.VarChar).Value = NRICNo.Value
            cmd.Parameters.Add("@Gender", SqlDbType.Decimal).Value = Gender.Value

            cmd.Parameters.Add("@DOB", SqlDbType.VarChar).Value = DOB.Value
            cmd.Parameters.Add("@BirthPlace", SqlDbType.VarChar).Value = BirthPlace.Value
            cmd.Parameters.Add("@Race", SqlDbType.Decimal).Value = Race.Value

            cmd.Parameters.Add("@ImageType", SqlDbType.VarChar).Value = "image/.jpg"
            cmd.Parameters.Add("@ImageData", SqlDbType.VarChar).Value = Photo.Value
            cmd.ExecuteNonQuery()

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Error occured : " & ex.Message.ToString() & "');", True)
        Finally
            cmd = Nothing
            conn.Close()
        End Try
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        InsertNRICData()
    End Sub
End Class

 

Posted one year ago

Hi maideen,

To retrieve the input values you have to Request.Form collection.

Check the below code.

C#

protected void Save(object sender, EventArgs e)
{
    InsertNRICData();
}
private void InsertNRICData()
{
    string name = Request.Form["Name"];
    string nRICNo = Request.Form["NRICNo"];
    decimal gender = Convert.ToDecimal(Request.Form["Gender"]);
    string dOB = Request.Form["DOB"];
    string birthPlace = Request.Form["BirthPlace"];
    decimal race = Convert.ToDecimal(Request.Form["Race"]);
    string photo = Request.Form["Photo"];
}

VB.Net

Private Sub InsertNRICData()
    Dim name As String = Request.Form("Name")
    Dim nRICNo As String = Request.Form("NRICNo")
    Dim gender As Decimal = Convert.ToDecimal(Request.Form("Gender"))
    Dim dOB As String = Request.Form("DOB")
    Dim birthPlace As String = Request.Form("BirthPlace")
    Dim race As Decimal = Convert.ToDecimal(Request.Form("Race"))
    Dim photo As String = Request.Form("Photo")
End Sub

Private Sub Save(sender As Object, e As EventArgs) Handles btnSave.Click
    InsertNRICData()
End Sub