日期:2014-05-18  浏览次数:21091 次

跪求解决问题的答案 !!急啊 !哪位前辈指点迷津一下!
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class User_Create : System.Web.UI.Page
{
  DBClass dbOcj = new DBClass();
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void CQBtn_Click(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  SqlConnection conn = dbOcj.GetConnection();
  try
  {
  conn.Open();
  SqlCommand sqltext = new SqlCommand("insert into [_User] values (@Name,@Possword,@真实姓名,@性别,@地址,@联系电话)", conn);
  SqlCommand sqlselect = new SqlCommand("select count(*) from [_User] where Name=@Name");
  sqltext.Parameters[0].Value = CNameTextBox.Text.Trim();
  sqltext.Parameters[1].Value = CPosswordTextBox.Text.Trim();
  sqltext.Parameters[2].Value = YournameTextBox.Text.Trim();
  sqltext.Parameters[3].Value = ListBox1.SelectedValue;
  sqltext.Parameters[4].Value = AdressTextBox.Text.Trim();
  sqltext.Parameters[5].Value = PhoneTextBox.Text.Trim();
  int count = Convert.ToInt32(sqlselect.ExecuteScalar());
  if (count > 0)
  {
  Response.Write("<script>alert('该用户已被注册!')</script>");
  }
  else
  {
  if(sqltext.ExecuteNonQuery())
  {
  Response.Write("<script>alert('注册失败!')</script>");


  }
  else
  {
  Response.Write("<script>alert('注册成功!')</script>");

  }
  }

  }
  finally
  {
  conn.Close();
  }

  }
  }
}
错误:错误 1 无法将类型“int”隐式转换为“bool” E:\Webshop\User\Create.aspx.cs 45 24 E:\Webshop\


------解决方案--------------------
if(sqltext.ExecuteNonQuery() == 0)
------解决方案--------------------
错误信息给的那么明显还找不到问题?行号都告诉你,我真服了
if(sqltext.ExecuteNonQuery())
这个sqltext.ExecuteNonQuery()方法返回的是int吧,C#的if只能判断bool,不能判断int。

 
------解决方案--------------------
if(sqltext.ExecuteNonQuery()) 改成 if(sqltext.ExecuteNonQuery()!=0)
------解决方案--------------------
探讨
if(sqltext.ExecuteNonQuery()) 改成 if(sqltext.ExecuteNonQuery()!=0)

------解决方案--------------------
int count = Convert.ToInt32(sqlselect.ExecuteScalar());
if(sqltext.ExecuteNonQuery())
你的这两句代码明显有冲突。sqlselect.ExecuteScalar()方法的返回值如果是bool值的话那么int count = Convert.ToInt32(sqlselect.ExecuteScalar());这句就会报错,如果是int行的话if(sqltext.ExecuteNonQuery())if条件句就会报错