Tuesday, February 14, 2012

Connect to the DB?

Hi,

I am a beginner in asp.net so sorry for my question ..

I want to make the event for the button when clicked , inside this event I will be transfer the value of dropdownlist into the DB by use ( WHERE ) condition ..

My question is :

- how I write the connection to the DB ?? is this a connection statement in the web.config ?? or what ?? because it generate an error by this code..

SqlConnection sqlConnection1 =new System.Data.SqlClient.SqlConnection("Data Source=-\SQLEXPRESS;Initial Catalog=2C2Mdb;Integrated Security=True");

SqlCommand cmd =newSqlCommand();

cmd.CommandType = System.Data.CommandType.Text;cmd.CommandText ="INSERT Meeting (mSessionNO) VALUES (" + DropDownList2.selectedvalue +") WHERE [HijriYear]=" + DropDownList1.SelectedValue ;

cmd.Connection = sqlConnection1;

sqlConnection1.Open();

cmd.ExecuteNonQuery ();

sqlConnection1.Close();

What is the proplem ?

There are a few things with the code

Add the @. symbol in front of the string so that it escapes the escape characters. \ is an escape character in C#

SqlConnection sqlConnection1 =new System.Data.SqlClient.SqlConnection(@."Data Source=-\SQLEXPRESS;Initial Catalog=2C2Mdb;Integrated Security=True");

You need to put quotes around your dropdownlist values

cmd.CommandType = System.Data.CommandType.Text;cmd.CommandText ="INSERT Meeting (mSessionNO) VALUES (' " + DropDownList2.selectedvalue +"') WHERE [HijriYear]='" + DropDownList1.SelectedValue +" ' ";

|||

Hi Sofy,

Everything seems correct except your ConnectionString.

Try this

SqlConnection sqlConnection1 =new System.Data.SqlClient.SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=2C2Mdb;Integrated Security=True");

Let me know if it works

Regards

|||

Thaaaanks for all replies ...

No comments:

Post a Comment