孟老大,思哥,吴旗兄等高手来看看:
本人做会员注册程序,其实很简单的,会员id,密码和会员姓名是必填项,年龄和所在诚市是可选项,提交数据后,程序执行有错,提示:”对象不能从 DBNull 转换为其他类型“,搞了三天了,不知如何搞??请各位大哥指导一下,我把我的设置再说一下:会员id,密码和会员姓名是必填项,其他字段不写也没事,在sqlserver数据库表中已经将可选项的字段加了默认值。程序是调用类和存储过程来实现的。存储过程如下:
CREATE PROCEDURE [insert_newuser]
(
@UserId [char](20),
@UserPassword [char](32),
@UserName [char](20),
@UserAge [int] ,
@UserCity [char](20),
@RegIp [char](20),
@Count1 int output
)
AS
SELECT @Count1 = COUNT (UserId) From UserInfo Where UserId = @UserIdIF(@Count1 > 0) RETURN @Count1
ELSE
BEGININSERT INTO [ UserInfo]
(
[UserId] ,
[UserPassword] ,
[UserName] ,
[UserAge] ,
[UserCity] ,
[RegIp]
)
VALUES
(
@UserId ,
@UserPassword ,
@UserName ,
@UserAge ,
@UserCity ,
@RegIp
)
RETURN @Count1 END
GO类文件部分原码如下:public int insertdata(string UserId,string UserPassword,string UserName,int UserAge,string UserCity,string RegIp)
{
//-----以下所有功能就是:在数据库中增加一条记录,增加一个用户OpenConn(); //打开数据库,当然是调用类;
SqlCommand cm=new SqlCommand("insert_newuser",Conn); //实例化存储过程对象,相关属性赋给cm,请注意,这里不能写Conn,要从类中调
cm.CommandType=CommandType.StoredProcedure; //当然操作类型为操作存储过程
try
{
cm.Parameters.Add(new SqlParameter("@UserId",SqlDbType.Char,20));//增加或实例化一个参数
cm.Parameters["@UserId"].Value=UserId;//给要传递的参数赋值cm.Parameters.Add(new SqlParameter("@UserPassword",SqlDbType.Char,32));
cm.Parameters["@UserPassword"].Value=UserPassword;cm.Parameters.Add(new SqlParameter("@UserName",SqlDbType.Char,20));
cm.Parameters["@UserName"].Value=UserName;cm.Parameters.Add(new SqlParameter("@UserAge",SqlDbType.Int,4));
cm.Parameters["@UserAge"].Value=UserAge;
cm.Parameters.Add(new SqlParameter("@UserCity",SqlDbType.Char,20));
cm.Parameters["@UserCity"].Value=UserCity;
cm.Parameters.Add(new SqlParameter("@RegIp",SqlDbType.Char,20));
cm.Parameters["@RegIp"].Value=RegIp;
cm.Parameters.Add("@Count1", SqlDbType.Int, 4);//RoleId就是要返回的参数,先传过去
cm.Parameters["@Count1"].Direction = ParameterDirection.Output;//大概是返回的参数名是:Count1
cm.ExecuteNonQuery();
}
catch
{ //如果有异常,则提示
//Response.Write ("<script>alert('增加记录有错!');</script>");
}
CloseConn();//关闭数据库
int flag = Convert.ToInt32(cm.Parameters["@Count1"].Value);//取回返回值
return flag; }
主程序文件RegUser.cs调用代码如下://以下功能,增加一个用户到数据表中//myclass.OpenConn(); //打开数据库,当然是调用类;
string UserId,UserPassword,UserCity,UserName,RegIp1;
int UserAge;
UserId=UserId1.Text;
UserName=UserName1.Text;UserPassword=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(UserPassword1.Text,"md5"); //以上这句为接收密码字符,且转为32MD5;
UserAge=Convert.ToInt32(UserAge1.Text.ToString());
UserCity=UserCity1.Text;
RegIp1=Request.ServerVariables["REMOTE_ADDR"];//用户注册时的ip地址
int flagint;
flagint=myclass.insertdata(UserId,UserPassword,UserName,UserAge,UserCity,RegIp1);
//以上这句调用类的insertdata过程,并把相关参数传过去,增加一条记录,并且把执行结果返回if (flagint>0)
{
//如果返回值>0,表示该用户名已经存在
Response.Write ("<script>alert('对不起,该会员名已经存在!');</script>");
}else
{
//如果<=0表示可以增加
Response.Write ("<script>alert('恭喜您注册成功!');location.href='User/index.aspx';</script>");
}
myclass.CloseConn();//调用类 Class1对象myclass的过程closeconn(),关闭数据库}程序运行全没问题!麻烦各位大哥了,谢谢,真的搞了三天了,也没搞过来!
本人做会员注册程序,其实很简单的,会员id,密码和会员姓名是必填项,年龄和所在诚市是可选项,提交数据后,程序执行有错,提示:”对象不能从 DBNull 转换为其他类型“,搞了三天了,不知如何搞??请各位大哥指导一下,我把我的设置再说一下:会员id,密码和会员姓名是必填项,其他字段不写也没事,在sqlserver数据库表中已经将可选项的字段加了默认值。程序是调用类和存储过程来实现的。存储过程如下:
CREATE PROCEDURE [insert_newuser]
(
@UserId [char](20),
@UserPassword [char](32),
@UserName [char](20),
@UserAge [int] ,
@UserCity [char](20),
@RegIp [char](20),
@Count1 int output
)
AS
SELECT @Count1 = COUNT (UserId) From UserInfo Where UserId = @UserIdIF(@Count1 > 0) RETURN @Count1
ELSE
BEGININSERT INTO [ UserInfo]
(
[UserId] ,
[UserPassword] ,
[UserName] ,
[UserAge] ,
[UserCity] ,
[RegIp]
)
VALUES
(
@UserId ,
@UserPassword ,
@UserName ,
@UserAge ,
@UserCity ,
@RegIp
)
RETURN @Count1 END
GO类文件部分原码如下:public int insertdata(string UserId,string UserPassword,string UserName,int UserAge,string UserCity,string RegIp)
{
//-----以下所有功能就是:在数据库中增加一条记录,增加一个用户OpenConn(); //打开数据库,当然是调用类;
SqlCommand cm=new SqlCommand("insert_newuser",Conn); //实例化存储过程对象,相关属性赋给cm,请注意,这里不能写Conn,要从类中调
cm.CommandType=CommandType.StoredProcedure; //当然操作类型为操作存储过程
try
{
cm.Parameters.Add(new SqlParameter("@UserId",SqlDbType.Char,20));//增加或实例化一个参数
cm.Parameters["@UserId"].Value=UserId;//给要传递的参数赋值cm.Parameters.Add(new SqlParameter("@UserPassword",SqlDbType.Char,32));
cm.Parameters["@UserPassword"].Value=UserPassword;cm.Parameters.Add(new SqlParameter("@UserName",SqlDbType.Char,20));
cm.Parameters["@UserName"].Value=UserName;cm.Parameters.Add(new SqlParameter("@UserAge",SqlDbType.Int,4));
cm.Parameters["@UserAge"].Value=UserAge;
cm.Parameters.Add(new SqlParameter("@UserCity",SqlDbType.Char,20));
cm.Parameters["@UserCity"].Value=UserCity;
cm.Parameters.Add(new SqlParameter("@RegIp",SqlDbType.Char,20));
cm.Parameters["@RegIp"].Value=RegIp;
cm.Parameters.Add("@Count1", SqlDbType.Int, 4);//RoleId就是要返回的参数,先传过去
cm.Parameters["@Count1"].Direction = ParameterDirection.Output;//大概是返回的参数名是:Count1
cm.ExecuteNonQuery();
}
catch
{ //如果有异常,则提示
//Response.Write ("<script>alert('增加记录有错!');</script>");
}
CloseConn();//关闭数据库
int flag = Convert.ToInt32(cm.Parameters["@Count1"].Value);//取回返回值
return flag; }
主程序文件RegUser.cs调用代码如下://以下功能,增加一个用户到数据表中//myclass.OpenConn(); //打开数据库,当然是调用类;
string UserId,UserPassword,UserCity,UserName,RegIp1;
int UserAge;
UserId=UserId1.Text;
UserName=UserName1.Text;UserPassword=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(UserPassword1.Text,"md5"); //以上这句为接收密码字符,且转为32MD5;
UserAge=Convert.ToInt32(UserAge1.Text.ToString());
UserCity=UserCity1.Text;
RegIp1=Request.ServerVariables["REMOTE_ADDR"];//用户注册时的ip地址
int flagint;
flagint=myclass.insertdata(UserId,UserPassword,UserName,UserAge,UserCity,RegIp1);
//以上这句调用类的insertdata过程,并把相关参数传过去,增加一条记录,并且把执行结果返回if (flagint>0)
{
//如果返回值>0,表示该用户名已经存在
Response.Write ("<script>alert('对不起,该会员名已经存在!');</script>");
}else
{
//如果<=0表示可以增加
Response.Write ("<script>alert('恭喜您注册成功!');location.href='User/index.aspx';</script>");
}
myclass.CloseConn();//调用类 Class1对象myclass的过程closeconn(),关闭数据库}程序运行全没问题!麻烦各位大哥了,谢谢,真的搞了三天了,也没搞过来!
某个字段是是Null值.即DBNull
int flagint;
flagint=myclass.insertdata(UserId,UserPassword,UserName,UserAge,UserCity,RegIp1);//以上这句调用类的insertdata过程,并把相关参数传过去,增加一条记录,并且把执行结果返回
是不是下面??
int flag = Convert.ToInt32(cm.Parameters["@Count1"].Value);//取回返回值
如果 是,改成nt flag = Convert.ToInt32("0" + cm.Parameters["@Count1"].Value.ToString());//取回返回值
这个有问题。
可以这样写
int flag = -1;
if(cm.Parameters["@Count1"] != DBNull.Value)
{
flag = Convert.ToInt32(cm.Parameters["@Count1"].Value);//取回返回值
}
另,我不知你要把dbnull转成什么.转成object肯定是可以的.
{
myDR.GetString("") //如果不为空才转换类型
}