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
Prabhu RajaPosted Sep 20, 2011, 8:46 AM
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
Hemant KumarPosted Sep 20, 2011, 8:16 AM
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.
sajid jebranPosted Sep 20, 2011, 8:13 AM
Pravin MorePosted Sep 20, 2011, 7:22 AM
use like this
"INSERT INTO Address(Name, Address)VALUES('+FName.Text+','+Address.Text+')";
Thanx,
Pravin.
Priya LingePosted Sep 20, 2011, 7:22 AM
Please try this.
string que = "Insert into Customer(ID,Name)" + " values ('" + txtfirst.Text + "','" + txtsecond.Text + "')";
Thanks.
Dorababu MekaPosted Sep 20, 2011, 7:16 AM