Solved

Add textbox text to database

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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