?
Solved

Add textbox text to database

Posted on 2006-06-17
5
Medium Priority
?
735 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 250 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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 .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

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