c# how to check for the database if username is not found.

Yee Keong 1 Reputation point
2022-10-26T03:23:55.657+00:00

string constr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\jettp\Downloads\MockTest\MockTest\Database1.mdf;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name, Weight FROM MyWeight where Name ='" + txt_Name.Text + "'"))

        {  
            cmd.CommandType = CommandType.Text;  
            cmd.Connection = con;  
            con.Open();  
            using (SqlDataReader sdr = cmd.ExecuteReader())  
            {  
                if (txt_Name.Text != null)  
                {  
                    sdr.Read();  
                    MessageBox.Show("Username is found");  


                    txt_Name.Text = sdr["Name"].ToString();  
                    txt_Weight.Text = sdr["Weight"].ToString();  
                    con.Close();  
                }  
                else  
                {  
                    lbl_WarningMsg.Text = "Name not found";  
                    con.Close();  

                }  

i tried using this command to search for the username which is in not found database, but database message kept saying that the name is found in the database. following by an error (System.InvalidOperationException: 'Invalid attempt to read when no data is present.'
)

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,391 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 113.1K Reputation points
    2022-10-26T07:24:32.3+00:00

    Try fixing the first lines of your if:

    . . .  
    if( sdr.Read() )  
    {  
       MessageBox.Show("Username is found");  
       . . .  
    

    By the way, to reduce the vulnerabilities, consider Parameterized Queries and do not use string concatenations.

    0 comments No comments

  2. Jiale Xue - MSFT 36,471 Reputation points Microsoft Vendor
    2022-10-26T07:25:26.96+00:00

    Hi @Yee Keong , Welcome to Microsoft Q&A.

    You are getting this error because your judgment is wrong.

    When searching for a non-existent name, you can still enter the if, so it will show that it is found, and then index the non-existing value, resulting in an error(Invalid attempt to read when no data is present).

    Use if (sdr.HasRows).

    You could try the following code to get what you wanted.:

    using (SqlDataReader sdr = cmd.ExecuteReader())  
    {  
    	if (sdr.HasRows)  
    	{  
    		sdr.Read();  
    		//  
    		MessageBox.Show("Username is found");  
    		//  
    		con.Close();  
    	}  
    	else  
    	{  
    		//  
    		con.Close();  
    	}  
    }  
    

    Regarding this verification, I have an answer on StackOverflow that you can refer to:

    Simple login and verification

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments