Till now I have learnt how to add, edit and delete data, But I am confused on how to add data from a 'Select' element into the database table and also from 'checkboxes'
For example: I have a Date Of Birth(DOB) column in my database, I need to add data from the HTML form select element to the DOB column of my database.
Below is my code for DOB.
Whole of the select statement has a name. The options have just the values. You dont need to give them a name. Your select statement has a value selected from options. thats the value of that div or whatever it is inside.
In your INSERT INTO clause. just use the variable for the <select>
var Month_Date = Request.Form["DOB_Month"];
// Others here too.
In the INSERT INTO statement just use the variables.
For second answer you need to use the IN() clause for example you have a form with some checkboxes you need to name them same. And give them different values. Than while in SELECT statement. Use the variable for Request.Form for the checkbox input names.
Then use IN() for them. This will give you the values. Now you can use the values in the INSERT INTO statement.
I guess you are having the WebMatrix try out your SQL statements in your Database workspace.
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
Thanks Mike, I have already followed that tutorials and it helped me a lot.
I am able to add form data to my database.
What I am trying to do is concatenate " DOB_Day + DOB_Month + DOB_Year" into one string and then input that strng value into my database DOB column, but dont know how to do that.
For second answer you need to use the IN() clause for example you have a form with some checkboxes you need to name them same. And give them different values. Than while in SELECT statement. Use the variable for Request.Form for the checkbox input names.
Then use IN() for them. This will give you the values. Now you can use the values in the INSERT INTO statement.
I guess you are having the WebMatrix try out your SQL statements in your Database workspace.
Aafi
Member
32 Points
53 Posts
Inserting Data from HTML form into Database
Nov 13, 2012 08:56 PM|LINK
Hello All,
Till now I have learnt how to add, edit and delete data, But I am confused on how to add data from a 'Select' element into the database table and also from 'checkboxes'
For example: I have a Date Of Birth(DOB) column in my database, I need to add data from the HTML form select element to the DOB column of my database.
Below is my code for DOB.
How do I accomplish this. I am able to get the data inserted from below select element, which is very simple.
Question 2: How do I get the data from HTML form 'Checkbox' element .
For Example: There are multiple options like in the code below
When user selects multiple check boxes, how do I add the multiple selection into database table.
I hope I have mentioned my questions clearly, I am very new to Web Programming and have just started learning from forums and W3School website.
Thank you.
Mikesdotnett...
All-Star
154929 Points
19869 Posts
Moderator
MVP
Re: Inserting Data from HTML form into Database
Nov 14, 2012 04:52 AM|LINK
You should spend a little time reviewing the tutorial here: http://www.asp.net/web-pages/tutorials/introducing-aspnet-web-pages-2/form-basics
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Afzaal.Ahmad...
Contributor
2661 Points
1040 Posts
Re: Inserting Data from HTML form into Database
Nov 14, 2012 08:13 AM|LINK
Whole of the select statement has a name. The options have just the values. You dont need to give them a name. Your select statement has a value selected from options. thats the value of that div or whatever it is inside.
In your INSERT INTO clause. just use the variable for the <select>
In the INSERT INTO statement just use the variables.
For second answer you need to use the IN() clause for example you have a form with some checkboxes you need to name them same. And give them different values. Than while in SELECT statement. Use the variable for Request.Form for the checkbox input names. Then use IN() for them. This will give you the values. Now you can use the values in the INSERT INTO statement.
I guess you are having the WebMatrix try out your SQL statements in your Database workspace.
~~! FIREWALL !~~
Aafi
Member
32 Points
53 Posts
Re: Inserting Data from HTML form into Database
Nov 14, 2012 02:28 PM|LINK
Thanks Mike, I have already followed that tutorials and it helped me a lot.
I am able to add form data to my database.
What I am trying to do is concatenate " DOB_Day + DOB_Month + DOB_Year" into one string and then input that strng value into my database DOB column, but dont know how to do that.
Aafi
Member
32 Points
53 Posts
Re: Inserting Data from HTML form into Database
Nov 14, 2012 02:30 PM|LINK
Thanks Afzaal, That helped.
Aafi
Member
32 Points
53 Posts
Re: Inserting Data from HTML form into Database
Nov 14, 2012 03:09 PM|LINK
A little progress with concatenating strings, below is my code which works fine. Please correct if you see any problems with my code.
@{ var firstname=""; var lastname=""; var hobbies=""; var location=""; var pnotes=""; var musicgenre=""; var dob_month=""; var dob_day=""; var dob_year=""; var dob=""; if(IsPost){ firstname=Request["firstname"]; lastname=Request["lastname"]; hobbies=Request["hobbies"]; location=Request["location"]; pnotes=Request["pnotes"]; musicgenre=Request["musicgenre"]; dob_month=Request["dob_month"]; dob_day=Request["dob_day"]; dob_year=Request["dob_year"]; dob=string.Concat(dob_month, "/", dob_day, "/", dob_year); var db=Database.Open("UserDetails"); var insertcommand="INSERT INTO Details(firstname, lastname, hobbies, location, musicgenre, pnotes, dob) values (@0, @1, @2, @3, @4, @5, @6)"; db.Execute(insertcommand, firstname, lastname, hobbies, location, musicgenre, pnotes, dob); Response.Redirect("~/users/Profileupdate"); } }Afzaal.Ahmad...
Contributor
2661 Points
1040 Posts
Re: Inserting Data from HTML form into Database
Nov 14, 2012 03:36 PM|LINK
..Sorry for previous answer. Doesnt matter.
~~! FIREWALL !~~
Mikesdotnett...
All-Star
154929 Points
19869 Posts
Moderator
MVP
Re: Inserting Data from HTML form into Database
Nov 14, 2012 06:27 PM|LINK
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Aafi
Member
32 Points
53 Posts
Re: Inserting Data from HTML form into Database
Nov 14, 2012 06:38 PM|LINK
Thanks Mike,
Your website & google has helped a lot to clear doubts