Solved

Add textbox text to database

Posted on 2006-06-17
5
695 Views
Last Modified: 2008-01-09
Hi!

I'm creating a small chat and I've encountered a small problem.

I've made a textbox at the bottom and a submit button.
I want the user to fill in the text in the textbox and when the submit button is pressed the textbox text, time and username will be filled in the database.

How do I do that without the use of a GridView or DetailsView, only using the textbox and a submit button?

Thank you.
0
Comment
Question by:Amplificator
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:meomar
ID: 16927547
Hi,

The textbox information will be accessed from the Text property of the text box. You will need to respond to the click event of the submit button and update database with that as follows:

protected cmdSubmit_click(Object Sender, EventArg e)
{
  //string conn = "Connection string to database";
 // SqlCommand cmd = new SqlCommand();
//cmd.Connection = conn;
// cmd.Commandtext = "Insert into Table values (@text, @UserName,@Time)";
// cmd.Paramerts.add("@Text", TextBox1.Text);
//You get the idea ????

try
{
 conn.open();
cmd.ExcuteNonQuery();
}
catch(Exception ex)
{
 label.Text += ex.ToString();
}
finally{
cmd.close();
conn.close()
}


I hope this helps.





}
0
 

Author Comment

by:Amplificator
ID: 16927609
Can you please try with a more complete example?

I'm new to this..
0
 
LVL 3

Accepted Solution

by:
meomar earned 125 total points
ID: 16927697
protected cmdSubmit_click(Object Sender, EventArg e)
{
  string conn = "Connection string to database";
  SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
 cmd.Commandtext = "Insert into Table values (@text, @UserName,@Time)";
 cmd.Paramerts.add("@text", TextBox1.Text);
cmd.Paramerts.add("@UserName", "Name");
cmd.Paramerts.add("@Time", DateTime.Now.ToString());

try
{
 conn.open();
cmd.ExcuteNonQuery();
}
catch(Exception ex)
{
 label.Text += ex.ToString();
}
finally{
cmd.close();
conn.close()
}
0
 

Author Comment

by:Amplificator
ID: 16928039
I've fixed it now, although I'm using a SqlDataSource, but pretty much the same way.

One last thing, when I add something the date is shown like this: 01-01-1900 01:29:44
The time is correct, but the date is wrong. I've had this problem before but I can't remember hwo to fix it.
0
 

Author Comment

by:Amplificator
ID: 16928107
I've found a solution.

SqlDataSource1.InsertParameters.Add("chatTime", TypeCode.DateTime, DateTime.Now.ToString());

I didn't have TypeCode.DateTime before, but it work perfekt with that.

Thank you for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now