首先我将一个新成员插入到成员表中。然后即时查询表以取回成员 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;
}