I have made connection in web.config file,Its working ,I want to just confirmed dat Whether i m doing it in right way or not!
Web.Config File-
<configuration>
<appSettings/>
<connectionStrings>
<add name="Connection" connectionString="Data Source=(local);Initial catalog=sonia;User ID=sonia;Password=sonia;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Code Behind Page
public partial class FrmPractise : System.Web.UI.Page
{
SqlConnection conn =new SqlConnection (ConfigurationManager.ConnectionStrings["Connection"].ConnectionString ) ;
string query;
SqlCommand cmd;
protected void btnInsertData_Click(object sender, EventArgs e)
{
try
{
query = "Insert into testing values(@EmpId,@EmpName)";
cmd=new SqlCommand (query ,conn ) ;
cmd .Parameters .AddWithValue ("@EmpId",txtEmpId .Text ) ;
cmd.Parameters .AddWithValue ("@EmpName",txtEmpName .Text ) ;
conn .Open ();
cmd.ExecuteNonQuery ();
conn .Close ();
}
catch (Exception ex)
{
lblError .Text =ex.Message .ToString ();
}
}
I have small probs more
query = "Insert into testing values('" & txtEmpId.Text & "','" & txtEmpName.Text & "')";
is there any error in the above line,I m getting Error -
Error 1 Operator '&' cannot be applied to operands of type 'string' and 'string'