0

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'

3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by sonia sardana
0

Try:

query = "Insert into testing values('" + txtEmpId.Text + "','" + txtEmpName.Text + "')"; query = "Insert into testing values('" + txtEmpId.Text + "','" + txtEmpName.Text + "')";

In C# you use + to concatenate strings, not the VB & operator.

0

Well you did it right! :)
Did the + operator fix the problem you were having with the string concatenation?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.