0

I'm getting this error:

Invalid object name 'Members'

on this line:

int MemberExist = (int)check_Member.ExecuteScalar();

With my current DB which looks like this: http://imgur.com/6fyKGn3 Why wouldn't this be executing?

Code:

// Get the connection 
SqlConnection DBConnection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=""E:\DS\Prac5\Part1\App_Data\MyDatabase.mdf"";Integrated Security=True");

SqlDataReader Reader = null;

DBConnection.Open();
SqlCommand check_Member = new SqlCommand("SELECT * FROM Members WHERE MembershipID = FirstName AND LastName = @Txtnput", DBConnection);
check_Member.Parameters.AddWithValue("@Txtnput", txtMembershipid.Text);

int MemberExist = (int)check_Member.ExecuteScalar();
1
  • change Mydatabase.mdf to Database.mdf and remove " in between check my answer below Commented May 13, 2015 at 6:07

4 Answers 4

1

seems some error in your connection string.

Data Source=(LocalDb)\v11.0;Initial Catalog=Database;Integrated Security=SSPI;AttachDBFilename='E:\DS\Prac5\Part1\App_Data\MyDatabase.mdf'

You can also use

"SELECT TOP 1 MembershipID  FROM Members WHERE MembershipID = FirstName AND LastName = @Txtnput"

Make Sure your Table name is Members. You can check that in the Model or Database.

bool IsMemberExist;
object MembershipId = check_Member.ExecuteScalar();

if(MembershipId  != null )
 {
   IsMemberExist = true;
  //Do other things here.  //Convert.ToInt32(MembershipId) 
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately my table is most definitely the correct name as you can see by my imgur link I posted.
Ahh yes thank you my connection string was incorrect.
1

Please make sure that whether you database MyDatabase.mdf contain the table named Members. Please test with following.

SqlCommand check_table= "select case when exists((select * from information_schema.tables where table_name = '" + tableName + "')) then 1 else 0 end");
int IsTableExists = (int)check_table.ExecuteScalar();
if(IsTableExists ==1)
{
        SqlCommand check_Member = new SqlCommand("SELECT * FROM Members WHERE MembershipID = FirstName AND LastName = @Txtnput", DBConnection);
        check_Member.Parameters.AddWithValue("@Txtnput", txtMembershipid.Text);
        int MemberExist = (int)check_Member.ExecuteScalar();
}

UPDATE: can you please update the query with schema name along with table name.

eg: SqlCommand check_Member = new SqlCommand("SELECT * FROM dbo.Members WHERE MembershipID = FirstName AND LastName = @Txtnput", DBConnection);

Or more specifically MyDatabase.dbo.Members

1 Comment

it does as far as i'm aware :/
0

It looks like you have to change the queries to become

"SELECT count(*) FROM Members WHERE MembershipID = FirstName AND LastName = @Txtnput"

Because ExecutesScalar expects a single value return from a query.

1 Comment

Hmm I just tried that but it's still giving me same exception. "Invalid object name 'Members'."
0

Try this connection string and check

    SqlConnection DBConnection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;
AttachDbFilename=E:\DS\Prac5\Part1\App_Data\Database.mdf;Integrated Security=True")

remove the " in between no need because start with @ also change MyDatabase.mdf to Database.mdf because in picture it is Database.mdf

1 Comment

@user4814432 check this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.