Reputation: 51
I have tried so many ways to save textbox text into an SQL database. I just want to save some text from textboxes into an SQL database. I am using Microsoft Visual Studio 2010 and C# programming language. Here is my current code:
protected void BtnSave_Click(object sender, EventArgs e)
{
string Name = TxtFirstName.Text;
string Address = TxtDOB.Text;
string DOB = TxtDOB.Text;
string Occupation = TxtOccupation.Text;
string Home = TxtHome.Text;
string Work = TxtWork.Text;
string Mobile = TxtMobile.Text;
string Email = TxtEmail.Text;
string SportActivities = TxtSportActivities.Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientsConnectionString"].ConnectionString)) ;
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO [ClientDetails] ([Name], [Address], [DOB], [Occupation], [Home], [Work], [Mobile], [Email], [SportActivities]) VALUES ( @Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities)")) ;
{
cmd.connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Name", TxtFirstName.Text);
cmd.Parameters.AddWithValue("@Address", TxtAddress.Text);
cmd.Parameters.AddWithValue("@DOB", TxtDOB.Text);
cmd.Parameters.AddWithValue("@Occupation", TxtOccupation.Text);
cmd.Parameters.AddWithValue("@Home", TxtHome.Text);
cmd.Parameters.AddWithValue("@Work", TxtWork.Text);
cmd.Parameters.AddWithValue("@Mobile", TxtMobile.Text);
cmd.Parameters.AddWithValue("@Email", TxtEmail.Text);
cmd.Parameters.AddWithValue("@SportActivities", TxtSportActivities.Text);
cmd.ExecuteNonQuery();
con.close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string Name = TxtFirstName.Text;
string Address = TxtDOB.Text;
string DOB = TxtDOB.Text;
string Occupation = TxtOccupation.Text;
string Home = TxtHome.Text;
string Work = TxtWork.Text;
string Mobile = TxtMobile.Text;
string Email = TxtEmail.Text;
string SportActivities = TxtSportActivities.Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientDetailsConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO [ClientDetails] ([ID], [Name], [Address], [DOB], [Occupation], [Home], [Work], [Mobile], [Email], [SportActivities]) VALUES (@ID, @Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities)");
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@ID", TxtID.Text);
cmd.Parameters.AddWithValue("@Name", TxtFirstName.Text);
cmd.Parameters.AddWithValue("@Address", TxtAddress.Text);
cmd.Parameters.AddWithValue("@DOB", TxtDOB.Text);
cmd.Parameters.AddWithValue("@Occupation", TxtOccupation.Text);
cmd.Parameters.AddWithValue("@Home", TxtHome.Text);
cmd.Parameters.AddWithValue("@Work", TxtWork.Text);
cmd.Parameters.AddWithValue("@Mobile", TxtMobile.Text);
cmd.Parameters.AddWithValue("@Email", TxtEmail.Text);
cmd.Parameters.AddWithValue("@SportActivities", TxtSportActivities.Text);
cmd.ExecuteNonQuery();
con.Close();
}
I have fixed my code. All i did was change 'CodeBehind' to CodeFile' in the aspx.cs file and fixed up some minor errors. Here is my working code: All i did was remove the brackets as the code didnt recognise 'cmd' and added the 'ID' field as that was the primary key of my database so it had to be there. Everything works great. Thanks for your help.
Upvotes: 2
Views: 11004
Reputation: 11
I think you have lost the link between the button press and the event. You can check this using the Properties Window.
My guess is that you have lost the link. So, comment out the event response code and recreate the event. Use the same process you used to create the event originally. Then, cut and paste the commented out code you need from the commented out event method. Remove the comment indicators from the pasted code. Your event should now work.
Upvotes: 0
Reputation: 2306
You should add parameter for each of these values:
@Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities
You used cmd.Parameters.AddWithValue("@Name", SqlDbType.Text);
only for name.
EDIT:
There are 2 ways to provide data to sql: 1) Adding parameter by name and later it's value:
command.Parameters.Add("@name", SqlDbType.Int);
command.Parameters["@name"].Value = nametextbox.text;
2) Adding parameter with value
command.Parameters.AddWithValue("@name", nametextbox.text);
Please Use one of them. Not the mix.
EDIT REGARDING ERROR:
If you use OnClick attribute in the page's HTML view, the name is expected to be on the page script and not in the code-behind. If you want to use a handler in the code-behind, remove OnClick attribute, go to design view and double-click the button. The code-behind will open on the right function. Or you can select event handler in the button properties panel on Events tab. Or you can do it without design view whatsoever. Just go to the code-behind and add a line for registering the event-handling method for the Click event of the button: btnSignIn.Click += new EventHandler(this.btnSignIn_Click);
When you do it in the design view, this line is added to the automatically generated InitializeComponent() method.
Upvotes: 3
Reputation: 1030
Click in front of brackets, a red point will come there and again start your process
Upvotes: 0