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'

Recommended Answers

All 5 Replies

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.

If it is workinh fine then you must have done it the right way..
Don't doubt yourself dear...

But i m newbie to ASP,dats y doubt is there!!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.