Hello Again,
Today i got a new problem, i want to add data of a text box to a database.the columns in database are (FName, Address) the names of the text boxes are FName and Address. the insert string for this is
INSERT INTO Address(Name, Address)VALUES('FName.Text','Address.Text'");
but when i see the table instead of the vale of textbox it is FName.text and Address.Text. i tried another time with string like this
"INSERT INTO Address (Name, Address) VALUES ('" + FName.Text + "','" + Address.Text + "')";
but this time it was the same
Loading

Dorababu MekaRank #457 · 5.5K reputationPosted Sep 20, 2011, 3:16 AM EDT
Priya LingeRank #785 · 2.9K reputationPosted Sep 20, 2011, 3:22 AM EDT
Please try this.
string que = "Insert into Customer(ID,Name)" + " values ('" + txtfirst.Text + "','" + txtsecond.Text + "')";
Thanks.
Pravin MoreRank #1,419 · 1.2K reputationPosted Sep 20, 2011, 3:22 AM EDT
use like this
"INSERT INTO Address(Name, Address)VALUES('+FName.Text+','+Address.Text+')";
Thanx,
Pravin.
sajid jebran14 reputationPosted Sep 20, 2011, 4:13 AM EDT
Hemant KumarRank #1,202 · 1.5K reputationPosted Sep 20, 2011, 4:16 AM EDT
You can insert the data like this please use this method
public bool SaveData(string name, string address)
{
try
{
String connectionString = ConfigurationManager.ConnectionStrings["TESTDB"].ConnectionString;
bool result = false;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
// "INSERT INTO Address(Name, Address)VALUES('+FName.Text+','+Address.Text+')";
String Query = "INSERT INTO Address(Name, Address)VALUES(@Name,@Address)";
cmd.CommandText = Query;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Address", address);
connection.Open();
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
result = true;
}
else
{
result = false;
}
}
return result;
}
catch (Exception exp)
{
throw exp;
}
}
Just try to pass the textbox values to the method .If verything gone good you will get the result as true else false.
Prabhu RajaRank #861 · 2.6K reputationPosted Sep 20, 2011, 4:46 AM EDT
Try this.
String Query = "INSERT INTO Address(Name, Address)VALUES(@Name,@Address)"; // Parameter for name and address
cmd.CommandText = Query;
cmd.Parameters[ "@Name" ].Value = this.FName.Text.Trim(); //Pass Values to Parameter
cmd.Parameters[ "@Address"].Value = this.Address.Text.Trim();
int n = cmd.ExecuteNonQuery();
-------------------------------------------------------------------------
Thank You