Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
C#
Line 29:         con.Open();
Line 30:         cmd = new SqlCommand("insert into login values('"+TextBox1.Text+"','"+TextBox3.Text+"'", con);
Line 31:         cmd.ExecuteNonQuery();
Line 32:     }
Line 33: 


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    SqlConnection con = new SqlConnection("Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");
    SqlCommand cmd;
    SqlDataReader dr;

    protected void Button1_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand("insert into login values('"+TextBox1.Text+"','"+TextBox3.Text+"'", con);
        cmd.ExecuteNonQuery();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand("select * from login where UserName='" + TextBox1.Text + "'", con);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Label1.Text = "UserName alredy Exist";
            this.Label1.ForeColor = Color.Red;
        }
        else
        {
            Label1.Text = "UserName is Avilable";
        }
    }
}
Posted
Updated 7-Feb-12 19:39pm
v2
Comments
ckulasekaran 8-Feb-12 1:39am    
Error in insert command or any where?
rockpune 8-Feb-12 1:44am    
exception
Abey Thomas 8-Feb-12 1:39am    
No code formatting, no explanation of what the error is.. how do someone help you?
rockpune 8-Feb-12 1:44am    
exception
Abey Thomas 8-Feb-12 1:47am    
what is the exception....?! please provide a dump of that.

Make sure your field names match the string value type that you are passing in for the insert here - new SqlCommand("insert into login values('"+TextBox1.Text+"','"+TextBox3.Text+"'", con);.
 
Share this answer
 
Comments
rockpune 8-Feb-12 2:09am    
Incorrect syntax near the keyword 'values'.
Server Error in '/Checkavailabulity' Application.
Incorrect syntax near the keyword 'values'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'values'.

Source Error:

Line 30:
Line 31: cmd = new SqlCommand("insert into values('"+TextBox1.Text+"','"+TextBox3.Text+"'", con);
Line 32: cmd.ExecuteNonQuery();
Line 33: }
Line 34:
C#
cmd = new SqlCommand("insert into login values('"+TextBox1.Text+"','"+TextBox3.Text+"'", con);


At least there is no ')'. Try something like this:
C#
cmd = new SqlCommand("insert into login values(@val1, @val2)", con);
cmd.Parameters.AddWithValue("@val1", TextBox1.Text);
cmd.Parameters.AddWithValue("@val2", TextBox3.Text);
 
Share this answer
 
Hi,

Make sure the values in your Login Table is equal to values to enter upon login.

If you have 2 Columns in your Login table you must have 2 parameters to past via codebehind.

This is correct:
cmd = new SqlCommand("insert into login values(@val1, @val2)", con);
cmd.Parameters.AddWithValue("@val1", TextBox1.Text);
cmd.Parameters.AddWithValue("@val2", TextBox3.Text);

But if you want to identify the specific column fields:
cmd = new SqlCommand("insert into login (username, password, dates) values(@val1, @val2, @val3)", con);
cmd.Parameters.AddWithValue("@val1", TextBox1.Text);
cmd.Parameters.AddWithValue("@val2", TextBox3.Text);
cmd.Parameters.AddWithValue("@val3", DateTime.Now());

I hope this one can help.

Thank You
 
Share this answer
 
Comments
rockpune 8-Feb-12 4:44am    
thank u brother
Specify The Field Names In The Insert Query

For Eg:

C#
INSERT INTO LOGIN (FieldName1, FieldName2) VALUES ('" + TextBox1.Text + "', '" + TextBox3.Text + "')
 
Share this answer
 
v2

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