app-code  里面的内容://验证数据库
    public static bool isused(string accmd)
    {
        OleDbConnection oledbconn = myconn();
        oledbconn.Open();
        OleDbCommand mycomm = new OleDbCommand(accmd, oledbconn);
        int i = Convert.ToInt32(mycomm.ExecuteReader());
        oledbconn.Close();
        return i > 0 ? true : false;
    }点击按钮的内容:protected void Button1_Click(object sender, EventArgs e)
    {
        string accmd = "select 用户名 from[用户信息表]where 1=1='" + this.TextBox1.Text + "'";
        if (dbconn.isused(accmd))
        {
            Response.Write("<script language='javascript'>window.alert('次ID已经被注册不可用')</script>");
        }
    }
  问题是:
无法将类型为“System.Data.OleDb.OleDbDataReader”的对象强制转换为类型“System.IConvertible”。 if (dbconn.isused(accmd))  这行 是红色的  大家帮帮忙 谢谢   我是 大菜鸟

解决方案 »

  1.   

    int i = mycomm.ExecuteNonQuery();
    用这个方法
    这个得到结果是影响的行数
    如果影响的行数是0就是不存在
    如果影响的行数大于0就存在
      

  2.   

    发一个ajax的<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxTest.aspx.cs" Inherits="ajax_AjaxTest" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
        var xmlhttp;
        function Validation()
        {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        var name=document.getElementById("UserName");
        xmlhttp.open("post","AjaxTest.aspx?userName="+name.value);
        xmlhttp.onreadystatechange=OnMessageback;
        xmlhttp.send(null);
        }
        function OnMessageback()
        {
        if(xmlhttp.readystate==4&&xmlhttp.status==200)
        {
        document.write(xmlhttp.responsetext);
        }
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <input type="text" id="UserName" onchange="Validation()" />
        </div>
        </form>
    </body>
    </html>using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;public partial class ajax_AjaxTest : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = Request.QueryString["userName"];
            if (!string.IsNullOrEmpty(userName))
            {
                string connString = "server=.;database=GameCardSale;user id=sa;password=123456";
                SqlConnection conn=new SqlConnection(connString);
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "select count(*) from UserInfo where userId=@userId";
                cmd.Parameters.Add("@userId", SqlDbType.NVarChar, 50).Value = userName;
                conn.Open();
                int count = (int)cmd.ExecuteScalar();
                conn.Close();
                if (count>0)
                {
                    Response.Write("用户名已存在!");
                }
                else
                {
                    Response.Write("恭喜你,此用户名可以使用!");
                }
            }
            
        }
        
    }
      

  3.   

    ajax异步获取数据,查询用户数据 
    <script type="text/javascript"> 
        var xmlHttp; 
        function createXMLHttpRequest() 
        { 
            if(window.ActiveXObject) 
            { 
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
            } 
            else if(window.XMLHttpRequest) 
            { 
                xmlHttp = new XMLHttpRequest(); 
            } 
        } 
        function CheckUserName() 
        { 
          var us=document.getElementById("txtname").value; 
          if(us!="") 
          { 
            createXMLHttpRequest(); 
            var url= "RegistValidate.ashx?username="+escape(document.getElementById("txtname").value); 
            xmlHttp.open("GET",url,true); 
            xmlHttp.onreadystatechange=ShowResult; 
            xmlHttp.send(null); 
            } 
        } 
        function ShowResult() 
        { 
            if(xmlHttp.readyState==4) 
            { 
                if(xmlHttp.status==200) 
                { 
                    var s; 
                    s=xmlHttp.responseText; 
                      alert(s); 
                } 
            } 
        } 
    </script> string sql = "select count(*) from [user] where username = '"+username.Text+"'"; 
    int count = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.Conn, CommandType.Text, sql)); 
    if (count > 0) 
        flag = true;'存在 
    else 
        flag = false;'不存在
      

  4.   

    可以啦~~  就是 点击按钮 怎么 没有 相应呢??   快好了  谢谢你们   protected void Button1_Click(object sender, EventArgs e)
        {
            string accmd = "select 用户名 from 用户信息表 where 用户名='" + this.TextBox1.Text + "'";
            if (dbconn.ExcuteSql(accmd))
            {
                Response.Write("<script language='javascript'>window.alert('次ID已经被注册不可用');</script>");
            }
        }
      

  5.   

    int i = Convert.ToInt32(mycomm.ExecuteReader()你这个方法用得和你的需求不对路
    你应该用ExecuteScalar()方法,误用了ExecuteReader,换一下就可以了