Solved

Add textbox text to database

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I build my own IDE using ASP.NET MVC? 2 43
Class object 2 39
Problem to refer to value 8 45
VB.net Move a class from Solution Items to a project 2 22
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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