First im inserting a new member into the members table. Then im querying the table to get back the Member id. I get the data into the table, but it does not apear there quick enough to do the query in the following lines.
I get this exception "ExecuteScalar requires an open and available Connection. The connection's current state is closed." I cant figure out whats wrong here.
//This code works fine
//Insert new members data
InsertMembers insert = new InsertMembers();
int age = Int32.Parse(txtAge.Text);
insert.InsertNewMember(txtEmail.Text, Myguid, txtName.Text, txtCity.Text, txtState.Text, txtDescription.Text, age, gender);
//This is the block thats failing
//Get Member Id to Insert into Pictures table
GetMemberInfo GetID = new GetMemberInfo();
int UMemberId = GetID.GetMemberId(Myguid);
Displayme.Text = UMemberId.ToString();
public int GetMemberID(string guid)
{
string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];
string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)";
int memberId;
using (var connection = new SqlConnection(strConectionString))
using (var command = new SqlCommand(StrSql, connection))
{
command.Parameters.Add("@GuidID", SqlDbType.VarChar).Value = guid;
memberId = (int)command.ExecuteScalar();
}
//returns 0 when it should be member id number
return memberId;
}