private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=sbdata;uid=sa;pwd=;");
con.Open();
if (this.tID.Text.Length!=15)
{
if (this.tID.Text.Length!=18)
{
this.dd.Text="请检查输入身份证位数是否正确";
}
}
else {int iS = 0;
string perIDSrc;
perIDSrc=tID.Text;
int[] iW=new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
string LastCode="10X98765432";
string perIDNew;
perIDNew=perIDSrc.Substring(0,6);
perIDNew += "19";
perIDNew += perIDSrc.Substring(6,9);
for( int i=0; i<17; i++)
{
iS += int.Parse(perIDNew.Substring(i,1)) * iW[i];
}
int iY = iS%11;
perIDNew += LastCode.Substring(iY,1);
tID.Text=perIDNew;}
SqlCommand cmd=new SqlCommand("select count(*) from personnel where IDNO='"+tID.Text.ToString()+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
Session["sfz"]=tID.Text;
Page.Response.Redirect("main.aspx");
}
else
{
this.dd.Text="输入身份证号码尚未投保";
}
con.Close();
}这段代码怎么回事啊,"请检查输入身份证位数是否正确"好像都不起作用的,只有"输入身份证号码尚未投保"这段语句起作用??
{
SqlConnection con=new SqlConnection("server=.;database=sbdata;uid=sa;pwd=;");
con.Open();
if (this.tID.Text.Length!=15)
{
if (this.tID.Text.Length!=18)
{
this.dd.Text="请检查输入身份证位数是否正确";
}
}
else {int iS = 0;
string perIDSrc;
perIDSrc=tID.Text;
int[] iW=new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
string LastCode="10X98765432";
string perIDNew;
perIDNew=perIDSrc.Substring(0,6);
perIDNew += "19";
perIDNew += perIDSrc.Substring(6,9);
for( int i=0; i<17; i++)
{
iS += int.Parse(perIDNew.Substring(i,1)) * iW[i];
}
int iY = iS%11;
perIDNew += LastCode.Substring(iY,1);
tID.Text=perIDNew;}
SqlCommand cmd=new SqlCommand("select count(*) from personnel where IDNO='"+tID.Text.ToString()+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
Session["sfz"]=tID.Text;
Page.Response.Redirect("main.aspx");
}
else
{
this.dd.Text="输入身份证号码尚未投保";
}
con.Close();
}这段代码怎么回事啊,"请检查输入身份证位数是否正确"好像都不起作用的,只有"输入身份证号码尚未投保"这段语句起作用??
我本来想这么判断的,不是15位,或不是18位就提示出错的
可是如果写成this.tID.Text.Length=15的话,就会出错::无法对属性或索引器“string.Length”赋值 -- 它是只读的
:无法将类型“int”隐式转换为“bool”
{
if (this.tID.Text.Length!=18)
{
this.dd.Text="请检查输入身份证位数是否正确";
}
}
else{执行数据库操作}
你输入15位的身份证号,当然可以执行数据库操作,如果不是15位,假如18位就什么都不输出了,不是18就输出"请检查输入身份证位数是否正确",也不会执行SQL语句
可是如果写成this.tID.Text.Length=15的话
--------------------------------------------------------------
大侠,应该是 this.tID.Text.Length == 15 (两个等于才是比较,一个等于变成赋值啦~)
另外最好取值时trim一下
this.tID.Text.Length=15 ——》this.tID.Text.Length == 15
int[] iW=new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
string LastCode="10X98765432";
string perIDNew;
perIDNew=perIDSrc.Substring(0,6);
perIDNew += "19";
perIDNew += perIDSrc.Substring(6,9);
for( int i=0; i<17; i++)
{
iS += int.Parse(perIDNew.Substring(i,1)) * iW[i];
}
int iY = iS%11;
tID.Text=perIDNew;
//执行数据库查询
SqlCommand cmd=new SqlCommand("select count(*) from personnel where IDNO='"+tID.Text.ToString()+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
Session["sfz"]=tID.Text;
Page.Response.Redirect("main.aspx");
}
else
{
this.dd.Text="输入身份证号码尚未投保";
}
con.Close();
}
else if (this.tID.Text.Length==18)
{
//执行数据库查询
SqlCommand cmd=new SqlCommand("select count(*) from personnel where IDNO='"+tID.Text.ToString()+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
Session["sfz"]=tID.Text;
Page.Response.Redirect("main.aspx");
}
else
{
this.dd.Text="输入身份证号码尚未投保";
}
con.Close();
}改过了,判断起作用了,呵呵,谢谢楼上的大侠们^^
现在的问题是,如果是15位,就转为18位,然后执行数据库操作
如果是18位,就直接进行数据库操作执行数据库操作的代码
SqlCommand cmd=new SqlCommand("select count(*) from personnel where IDNO='"+tID.Text.ToString()+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
Session["sfz"]=tID.Text;
Page.Response.Redirect("main.aspx");
}
else
{
this.dd.Text="输入身份证号码尚未投保";
}
con.Close();这一段,用了两次,能不能有让程序更简化一些呀,如要用跳转要怎么办,或者判断语句要怎么处理比较好呢^-^~~~