5
Contributors
5
Replies
19
Views
7 Years
Discussion Span
Last Post by SNK111
0

i have tried the code but the data don't get inserted help me
folling is the code

SqlCommand cmd;
SqlConnection con = new SqlConnection("Data Source=Prince-notebook;Initial Catalog=Jobbazar;Integrated Security=True;");
con.Open();
cmd = new SqlCommand("INSERT INTO employer VALUES('" + usrtxt.Text + "', '" + pwdtxt1.Text + "','" + emcl.Text + "','" + comtxt.Text + "','" + contacttxt.Text + "','" + type + "','" + DropDownList1.SelectedItem + "','" + addtxt1.Text + "','" + count1.SelectedValue + "','" + state1.Text + "','" + city1.Text + "','" + pin1.Text + "','" + phno.Text + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
0

as you written your code is write can you tell me what is error comes while inserting a command.i think in sqlDatabase it conficlt the datatype. but your code in debug mode or best way is write your code in try catch block. it will show which is the error in statement.

1

To connect to SQL Server, you need to create a connection string such as below:

private SqlConnection connection;
private string connectionString =
@"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123";
connection = new SqlConnection( connectionString );

Next, you use the SqlConnection object created above to create a 'SqlCommand', as shown below:
SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = @Cid", connection);

The SQL query shown here can be replaced by a SELECT, INSERT, UPDATE queries etc.

Next to execute the SQL queries in the database, you use the following methods:
ExecuteReader - to execute SELECT queries
ExecuteNonQuery - to execute INSERT, DELETE, UPDATE, and SET statements.

This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database.
For details about the connection string, the methods and their parameters check the following link: ( )
Here you will also find details about how to pass parameters to the SQL queries as well as calling stored procedures and much more.

Edited by Ezzaral: Snipped promotional link. You've been told over and over this is not allowed.

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.