Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public partial class _Default : System.Web.UI.Page
{

    SqlConnection connectionString = new SqlConnection("Data Source=AZHAR-PC\\SQLEXPRESS;Initial Catalog=Emp_DB;Integrated Security=True");
    SqlCommand commandString;

    protected void Page_Load(object sender, EventArgs e)
    {
     connectionString.Open();
}
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        String query;
        query = "insert into SalesEmpTable values(" +txtId.Text+ ",'" + txtName.Text + "' , " + txtOrder + ")";

        commandString = new SqlCommand(query, connectionString);
        int record = commandString.ExecuteNonQuery();

        lblStatus.Text = "Status: Successfully Inserted";
        commandString.Dispose();
    }
}
Posted
Updated 13-Jan-14 23:40pm
v2
Comments
And what is that Exception?
Kornfeld Eliyahu Peter 14-Jan-14 5:41am    
And the error is?

You should really specify the columns in your insert statement:
SQL
INSERT INTO tablename (col1,col2col3) values ('a','b','c')

http://www.w3schools.com/sql/sql_insert.asp[^]
 
Share this answer
 
Replace
SQL
query = "insert into SalesEmpTable values(" +txtId.Text+ ",'" + txtName.Text + "' , " + txtOrder + ")";


with
SQL
query = "insert into SalesEmpTable values(" +txtId.Text+ "," + txtName.Text + " , " + txtOrder + ")";


You should use a parameter in query to insert rows in a table.
Refer:
http://www.vistadb.net/tutorials/insertrows-csharp.aspx[^]
This may help.
 
Share this answer
 
replace:-
query = "insert into SalesEmpTable values(" +txtId.Text+ ",'" + txtName.Text + "' , " + txtOrder + ")";


to

query = "insert into SalesEmpTable values(" +txtId.Text+ "," + txtName.Text + " , " + txtOrder + ")";
 
Share this answer
 
Thankyou guys.
I got my error: i have not added Text in txtOrder

The correct statement is

query = "insert into SalesEmpTable values(" +txtId.Text+ ",'" + txtName.Text + "' , " + txtOrder.Text + ")";
 
Share this answer
 
Comments
CHill60 14-Jan-14 11:39am    
I'm glad you have found the error. Now read Solution 1 and Solution 2 again ... you should really specify the columns (this would have made it easier to spot your problem) and you really really should NOT just append textbox text in your query - use Parameters
Some time i get this exception :
The multi-part identifier "System.Web.UI.WebControls.TextBox" could not be bound.

Code is :

querySelect = "Select EmpId, EmpName, EmpOrder from SalesEmpTable where EmpId = " + txtId + " ";

cmd = new SqlCommand(querySelect, con);

SqlDataReader dr = cmd.ExecuteReader(); ---i am getting above exception in this line
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900