Solved

Add textbox text to database

Posted on 2006-06-17
5
704 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

12 Experts available now in Live!

Get 1:1 Help Now