まず、メンバー テーブルに新しいメンバーを挿入します。次に、テーブルにクエリを実行して、メンバー ID を取得します。テーブルにデータを取得しましたが、次の行でクエリを実行するのに十分な速さで表示されません。
「ExecuteScalar には、オープンで使用可能な接続が必要です。接続の現在の状態は閉じています。」という例外が発生します。ここで何が悪いのかわかりません。
//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;
}