Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to retrieve a clinicID on the general table by the ClinicName and send the clinicID to the user when they register.

What I have tried:

int clinicId;
            string sqlClinicID = "SELECT ClinicID FROM GENERAL WHERE ClinicName ='" + txtClinicName.Text + "'";
             cmd = new SqlCommand(sqlClinicID, con);
             SqlDataReader rd = cmd.ExecuteReader();

            while (rd.Read())
            {
                clinicId = Convert.ToInt32(rd[0].ToString());
            }
            con.Close();
            
            sendMsg(clinicId, username, txtPassword.Text, email);
Posted
Updated 20-May-17 0:10am

For starters, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Secondly, if there is only one matching value in your DB why loop?

Thirdly, if it's an integer in your DB, why convert it to a string, in order to convert it back to an integer?

Fourthly, you need to Dispose SqlCommands, SqlReaders, SqlConnections when you are finished with them.

Try this:
int clinicId;
string sqlClinicID = "SELECT ClinicID FROM GENERAL WHERE ClinicName =@CN";
using (SqlCommand cmd = new SqlCommand(sqlClinicID, con))
   {
   cmd.Parameters.AddWithVaue("@CN", txtClinicName.Text); 
   clinicId = (int) cmd.ExecuteScalar();
   sendMsg(clinicId, username, txtPassword.Text, email);
   }
If that doesn't solve your problem, you need to explain in better detail what exactly is going wrong.


Thank it helped me but im getting an error on this line.
System.NullReferenceException: Object reference not set to an instance of an object.
C#
clinicId = (int)cmd.ExecuteScalar();

That's because there is no matching item - and your code doesn't check user input. When you don't allow the user to make mistakes, your code will fail - and users make mistakes all the time: heck I had to correct typos while I was typing this!
And when there is no match, SQL doesn't return any rows.

So always allow your users to make mistakes, and handle them gracefully:
C#
int clinicId = -1;
string sqlClinicID = "SELECT ClinicID FROM GENERAL WHERE ClinicName = @CN";
using (SqlCommand cmd = new SqlCommand(sqlClinicID, con))
    {
    cmd.Parameters.AddWithValue("@CN", txtClinicName.Text);
    object o = cmd.ExecuteScalar();
    if (o == null)
        {
        ... Report problem to user...
        return;
        }
    clinicId = (int)o;
    sendMsg(clinicId, username, txtPassword.Text, email);
    }
 
Share this answer
 
v2
Comments
Member 13147658 20-May-17 2:23am    
Thank it helped me but im getting an error on this line.

System.NullReferenceException: Object reference not set to an instance of an object.


clinicId = (int)cmd.ExecuteScalar();
Maciej Los 20-May-17 3:01am    
5ed!
I think this is better

<pre>int clinicId = -1;
string sqlClinicID = "SELECT ClinicID FROM GENERAL WHERE ClinicName = @CN";
using (SqlCommand cmd = new SqlCommand(sqlClinicID, con))
    {
    cmd.Parameters.AddWithValue("@CN", txtClinicName.Text);
    object result= cmd.ExecuteScalar();
    if (result.Length>0)
        {
        clinicId = (int)result;
        }else
{
// An error Occurs 
return
}
   
    sendMsg(clinicId, username, txtPassword.Text, email);
    }
 
Share this answer
 
v2
Comments
OriginalGriff 20-May-17 7:49am    
Reason for my vote of one: When you post a solution, it should work. It should at least compile.
ExecuteScalar does not return a string, it returns an object - so your code will fail compilation with "Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)"
Assuming that was fixed, Execute scalar returns a null if no items are found, so your if test will fail at run time with a null reference exception when you try to use the Length property of a null value.
Assuming that was fixed, you can't cast a string to an int even if it contains an integer value as a string - you have to either use Convert.ToInt32 (bad idea) or Parse / TryParse (good idea).

"Better" ideas are ones that work, not ones that don't even compile or run. Please, test your code before you post it as a solution in future.
Member 13147658 20-May-17 8:24am    
Thank you very much it worked.
Dave Kreskowiak 20-May-17 11:45am    
You better make sure it works in all cases, such as when the textbox is empty or has a string in it that doesn't exist in your database.

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