简体   繁体   中英

How to fix problem with textbox in Aspx.cs

Hello I have an html registration form, I opened it with html and checked the data with javascript now I want to insert my data to access database and I don't know how to do it because it's making me problems... I tried to write this aspx.cs : public void Register_Click(object sender, EventArgs e) { OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Vort3eX\\Desktop\\RegisterDataBase.mdb;Persist Security Info=True"); con.Open(); OleDbCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into DataBase value('"+fname.Text+ cmd.ExecuteNonQuery(); con.Close(); } public void Register_Click(object sender, EventArgs e) { OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Vort3eX\\Desktop\\RegisterDataBase.mdb;Persist Security Info=True"); con.Open(); OleDbCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into DataBase value('"+fname.Text+ cmd.ExecuteNonQuery(); con.Close(); }

And I got this error :

"The Name 'fname' does not exist the currect context"

My Html Code for my input

  <tr> <td class="style4"> Name </td> <td class="style3"> <input type="text"name="fname" id="fname" placeholder = "Name" /> </td </tr> 

Please help me to fix it I want to input data in my data access table...

Your server-side code has no way of recognizing your HTML input without adding runat="server" to the input element:

<input type="text" name="Name" id="fname" placeholder="Name" runat="server" />

To access the value of a HTML element in the code-behind, you need to use:

fname.Value

IMPORTANT: Make sure there is a form element or nothing will be submitted:

http://www-db.deis.unibo.it/courses/TW/DOCS/w3schools/aspnet/aspnet_forms.asp.html

You also need to be using query parameters in your code to avoid SQL Injection attacks:

Call a stored procedure with parameter in c#

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