Inserting data into a database from forms - c#

I am trying to take data which has been typed into input boxes and then save them back to my database. I am connecting to a dataset called password with a table called password. This is my connection
passTableAdapters.passwordsTableAdapter pass = new passwordTableAdapters.passwordTableAdapter();
password.passwordsDataTable passtable = pass.GetData();
This allows me to connect to the dataset but from there I am really stuck. I want to take the information from the textbox and then save them back to the dataset.
I have been using msdn but still no luck. If someone can show me how to write back to the dataset I would be very grateful.

To get the data from your HTML Controls you may try this logic. I am assuming that you have Two Control like
HTML:
<input id="username" name="username" type="text" />
<input id="password" name="password" type="password" />
Code Behind:
var username = Request.Form["username"];
var password = Request.Form["password"];
Note:
Never forget to Give the inputs a name as well as an id otherwise Request.Form will not work
By using this you may get the value from input tag and then pass these values to your function as a parameter in which you are using Insert Query.
Hope you understand.

It looks like you are using type data adapters for accessing the database. If this is the case you should have insert methods. If not you need to reconfigure this and tell XDS to generate those methods for you.
Retrieving data from your form is as easy as TextBoxName.Text .

Related

Read and wirte Data without use forms

I hope you can help me. I got a problem with reading and writing in and out from inputs in HTML.
Code:
<html>
<input id="test" value="" type="text"/>
</html>
in c# I access the value with:
test.value="test";
It works without any problems.
If i fill the Input on Page_load. I cant read the new value which was be entered by user.
I dont want to reload the Page.
Edit:
It dont solve my problem but it works too.
on Page_load
test.Attributes.Add("placeholder",value)
and on callback i read the new value with
test.value

How can I pass the Hidden Field value in SqlCommand in C# asp.net Three Tier Architecture?

I want to insert,update,delete values in db using stored procedure in three tier architecture.For that I am using the HiddenField to perform the above action.In order to perform the insert operation,I have to pass the value #Action='Insert' to stored Procedure by SQL Command.But in my code,I cant view the hiddenfield Id.can anyone help me do so.
My code as,
public void WorkingProgress(Workin_Variable V1)
{
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("#Action",HiddenfieldId.value)
}
Here HiddenFieldId not shown
Add runat="server" tag in your HiddenfieldId control. Also if you are not reading this in code behind file, read this in a variable and pass to ADO.NET function.
Anything you want to access from back end you need to add runat="server" then you can pass
If its table or Paragraph tag.
For this senerio
<asp:HiddenField ID="HiddenField" runat="server" value="" />
in back code access from HiddenField.Value

get value of hidden input field in c#

I have a charts program that works in js, but I need to update excels with the data using c#. So, what I've done so far is to create hidden input fields whose values are updated with the js variable:
<input type="hidden" name="baselineInput" id="baselineVariable" />
<input type="hidden" name="goalInput" runat="server" id="goalVariable" />
<input type="hidden" name="currentInput" runat="server" id="currentVariable" />
//do some things here
var MTDGauge = new Array(5,5,5);
document.getElementById("baselineVariable").value = MTDGauge[0];
document.getElementById("goalVariable").value = MTDGauge[1];
document.getElementById("currentVariable").value = MTDGauge[2];
The problem I'm having is reading these values back to the c# variable. Here's what I've tried (placed after the code above):
List<string> OverallData = new List<string>();
OverallData.Add(Request["baselineInput"]);
OverallData.Add(Request["goalInput"]);
OverallData.Add(Request["currentInput"]);
This is just adding null values (assuming so because the c# is loading before the javascript and the initialized values for the text fields are null?). I thought of creating a c# function that could be called using the onchange option in the input field, but that didn't get me anywhere. Is there something I'm missing? Do I have to do a post?
Thanks
Yes, to get your data back to the server (where your C# is running) you need to send it back.
POST is one way, or GET to add the values to the query string.
Javascript is only running in the browser, until you send something back to the server, your ASP.NET app will not have any information.
Of course, if you want to do it asynchronously you could use jQuery.ajax to send the data to a server URL without refreshing the whole page.

how to get <input of type=hidden> with FindControl

I am trying to get the value of a hidden input in code behind with the following code. I am trying to cast it but it cannot find it , any help ?
((HtmlControl)FindControl("contentId"))
I declare it in aspx with the following code:
<input id="contentId" type="hidden" />
I dont want to runat server because i have my own reasons
To access a HTML control at server side (in your C# code), you need to first add the runat="server" attribute. So, your markup should look like
<input type="hidden" id="contentId" runat="server"/>
Now, in the code behind you can use the control by its id contentId itself if the code behind got generated properly.
Please let us know why you are forced to use the FindControl in the first place as it can be accessed by using the id directly.
Update
As per the comment below, the user for some reason is not interested in making this input a server side control. Then the only possibility by which you can read the values at server side is as below. But this is not advised as any changes to the name goes unnoticed and breaks at runtime.
<input type="hidden" id="contentId" name="contentName" runat="server"/>
In Code
this.Request.Forms["contentName"] would return the hidden value.
Try to search it on the page this way
HiddenField hf = (HiddenField)page.FindControl("contentId");
To get the value:
HiddenField h = (HiddenField)Gridview.FindControl("HiddenFieldName");
Then with that you can put it into a string, if you wish to.
Use this code:
string s=((HiddenField)Panel1.FindControl("contentId")).Value;
Here panel is the container control. This may be a grid or anything else or even a master page. But if you are using FindControl, i think the control may be inside some container.
HtmlInputHidden hf = Page.FindControl("contentId") as HtmlInputHidden;
string hfValue = hf.Value;

Set the value of an input tag with type="password" using C# and ASP.Net

So I have this input tag:
<input type="password" id="password" name="password" runat="server" />
I'm trying to set its value using C# and ASP.Net before the page loads, but I can't get it to work.
Don't set the input type in your markup or ASP.NET will clear the input before it is rendered. Instead, use the following markup:
<input id="tbPassword" name="password" runat="server" />
...and the following code in your code-behind:
tbPassword.Value = "your password";
tbPassword.Attributes["type"] = "password";
However, I strongly advise against this approach. For one, you shouldn't be storing passwords in plain text. It's a really bad security practice.
Two, if you're just going to set the password in code, why require it at all? The point of a password input is for user interaction (entering a password). If you do that for them, then having it on the page is pointless.
You can set the value of <asp:TextBox> using TextBox.Attributes.Add, then set the value property for the textbox.
code sample:
TextBox1.Attributes.Add("value", "My Sample Password");
This is by design for security reasons. What are you trying to fill the textbox with? You're not storing user's passwords in plain text are you?
If you're talking about a user login form, and you have access to the unencrypted password, you're doing it WRONG.
User passwords should be stored as a cryptographic hash in your database, which cannot be reversed into the plaintext password, only checked for a match.
If what you want to do is preload the user's password in the input box to save him some typing, just don't do it, because the user's web browser will probably do a better job at that.
Have you tried something like this:
void Page_Load(object sender, eventargs e)
{
password.value= "something";
}

Categories