Click here to Skip to main content
15,891,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to validate if the first name and last name is already exist in my sql database i have error in temp = Convert.ToInt32(com.ExecuteScalar().ToString());

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["registrationConnectionString"].ConnectionString);
                conn.Open();

                
                string check = "select count(*) from userdata where FirstName,LastName='" +   TextBoxFN.Text + TextBoxFN.Text +"'";
                SqlCommand com = new SqlCommand(check, conn);
                com.Parameters.AddWithValue("@FN", TextBoxFN.Text);
                com.Parameters.AddWithValue("@LN", TextBoxLN.Text);
                temp = Convert.ToInt32(com.ExecuteScalar().ToString());

                if (temp == 1)
                {

                    Label1.Text = "Student Already Exist";

                }
Posted
Updated 28-Jan-15 18:03pm
v3
Comments
Rajesh waran 29-Jan-15 0:21am    
Then what is your problem? Label1 is showing error msg or not?
Member 11407981 29-Jan-15 1:58am    
not showing any error on label 1

Replace this line with
string check = "select count(*) from userdata where FirstName,LastName='" +   TextBoxFN.Text + TextBoxFN.Text +"'";


With this
C#
string check = string.formt("select count(*) from userdata where FirstName='{0}' AND LastName='{1}'", TextBoxFN.Text, TextBoxLN.Text);
 
Share this answer
 
Comments
Member 11407981 29-Jan-15 8:07am    
thankyou i solve my problem with your code thanks a lot
You need not to use parameterized query since your query already contains values. so make your query as follows and then run it.

conn.Open();
string inputQuery= "select count(*) from userdata where FirstName='" + TextBoxFN.Text         + "' And LastName='" + TextBoxFN.Text +"'";
SqlCommand com = New SqlCommand(inputQuery, conn);
temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp >= 1) '<--- updated here
  {
    Label1.Text = "Student Already Exist";
  }


or else you can use parameterized query as :

string check = "select count(*) from userdata where FirstName='{FN}' AND LastName='{LN}'"
SqlCommand com = new SqlCommand(check, conn);
com.Parameters.AddWithValue("@FN", TextBoxFN.Text);
com.Parameters.AddWithValue("@LN", TextBoxLN.Text);
temp = Convert.ToInt32(com.ExecuteScalar().ToString());
 
Share this answer
 
v3
Comments
Rajesh waran 29-Jan-15 0:32am    
This may leads to sql injection.Have a look at this link.
http://www.w3schools.com/sql/sql_injection.asp[^][^]
Rajesh waran 29-Jan-15 0:33am    
Just now saw your updated answer
Sujith Karivelil 29-Jan-15 0:41am    
now it solves the problem, isn't it?
Rajesh waran 29-Jan-15 0:55am    
Yes,passing parameterised Query is right one.
Member 11407981 29-Jan-15 1:56am    
i for got to say this code is in page load event, ill try your code but the problem is not validating in my sql database. and the label1 is not showing any error message. example database have a firstname (ron) and lastname is (logmao) when user registered at the same name my database have a duplicate name and last name

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