0

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

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

Alon
  • 687
  • 1
  • 6
  • 10
Roie
  • 3
  • 5

1 Answers1

1

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#

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • Hey thank you for the trying to help, I added runat="server" and it still showing me this problem... – Roie Oct 08 '18 at 18:09
  • The property for the text is different with HTML inputs. I will update answer. – IrishChieftain Oct 08 '18 at 18:13
  • Cant i use OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Vort3eX\Desktop\RegisterDataBase.mdb;Persist Security Info=True"); – Roie Oct 08 '18 at 18:19
  • https://stackoverflow.com/questions/2675610/how-to-update-a-table-using-oledb-parameters – IrishChieftain Oct 08 '18 at 18:21