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

1 Answer 1

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#

Sign up to request clarification or add additional context in comments.

4 Comments

Hey thank you for the trying to help, I added runat="server" and it still showing me this problem...
The property for the text is different with HTML inputs. I will update answer.
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");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.