本文介绍了自动生成用户ID时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好

我在这段代码中收到错误。

我正在尝试做的我使用
$ b生成自动递增用户ID $ b System.Guid U_id = System.Guid.NewGuid();系统类



 System.Guid U_id = System.Guid.NewGuid(); 
bjBusinessUI.UserID = Convert.ToInt32(U_id.ToString());
string strMsg = objDataUserUI.InsertUsers(objBusinessUI);. // 这是方法





//方法

公共字符串InsertUsers( Business.Users objUsers)

{

string retUserInsert = string.Empty;

try

{

// string conneString =Data Source = .; Initial Catalog = Vestigo; Integrated Security = True;

using(SqlConnection conn = Connection.OpenConnection())

{

// conn.Open();

SqlCommand cmd = new SqlCommand();

cmd.CommandText = INSERT_USER;

cmd.CommandType = System.Data.CommandType.StoredProcedure;

cmd.Connection = conn;

S qlParameter [] sqlParams = new SqlParameter [15];



SqlParameter UserName = new SqlParameter(@ UserName,objUsers.UserName);

UserName.Direction = System.Data.ParameterDirection.Input;

UserName.DbType = System.Data.DbType.String;

sqlParams [0] = UserName;



SqlParameter FirstName = new SqlParameter(@ FirstName,objUsers.FirstName);

FirstName.Direction = System.Data.ParameterDirection.Input;

FirstName.DbType = System.Data.DbType.String;

sqlParams [1] = FirstName;



SqlParameter LastName = new SqlParameter(@ LastName,objUsers.LastName);

LastName.Direction = System.Data.ParameterDirection.Input;

LastName.DbType = System 。数据。 DbType.String;

sqlParams [2] = LastName;



SqlParameter Password = new SqlParameter(@ Password,objUsers.Password) ;

Password.Direction = System.Data.ParameterDirection.Input;

Password.DbType = System.Data.DbType.DateTime;

sqlParams [3] =密码;



SqlParameter Email = new SqlParameter(@ Email,objUsers.Email);

Email.Direction = System.Data.ParameterDirection.Input;

Email.DbType = System.Data.DbType.String;

sqlParams [4] =电子邮件;



SqlParameter LastLogIn = new SqlParameter(@ LastLogIn,objUsers.LastLogIn);

LastLogIn.Direction = System.Data.ParameterDirection.Input;

LastLo gIn.DbType = System.Data.DbType.DateTime;

sqlParams [5] = LastLogIn;



SqlParameter CreatedBy = new SqlParameter( @CreatedBy,objUsers.CreatedBy);

CreatedBy.Direction = System.Data.ParameterDirection.Input;

CreatedBy.DbType = System.Data.DbType.String;

sqlParams [6] = CreatedBy;



SqlParameter CreatedOn = new SqlParameter(@ CreatedOn,objUsers.CreatedOn);

CreatedOn.Direction = System.Data.ParameterDirection.Input;

CreatedOn.DbType = System.Data.DbType.DateTime;

sqlParams [7] = CreatedOn ;



SqlParameter UpdatedBy = new SqlParameter(@ UpdatedBy,objUsers.UpdatedBy);

CreatedOn.Direction = System.Data.ParameterDirection 。输入;

CreatedOn.DbType = System.Data.DbType.String;

sqlParams [8] = updatedBy;



SqlParameter UpdatedOn = new SqlParameter(@ UpdatedOn,objUsers.UpdatedOn);

UpdatedOn.Direction = System.Data.ParameterDirection.Input;

UpdatedOn.DbType = System.Data.DbType.DateTime;

sqlParams [9] = UpdatedOn;



SqlParameter Active = new SqlParameter(@ Active, objUsers.Active);

Active.Direction = System.Data.ParameterDirection.Input;

Active.DbType = System.Data.DbType.Boolean;

sqlParams [10] =有效;





// SqlParameter SecurityQuestion =新的SqlParameter(@ SecurityQuestion,objUsers.SecurityQuestion );

//SecurityQuestion.Direction = System.Data.ParameterDirection.Input;

// SecurityQuestion = System.Data.DbType.DateTime;

// sqlParams [4] = SecurityQuestion ;



// SqlParameter答案=新的SqlParameter(@ Answer,objUsers.Answer);

//Answer.Direction = System .Data.ParameterDirection.Input;

//Answer.DbType = = System.Data.DbType.DateTime;

// sqlParams [4] =答案;





cmd.Parameters.AddRange(sqlParams);

cmd.ExecuteNonQuery();

//conn.Close();





}

}

catch(exception ex)

{



log.Error(System.Reflection.MethodBase.GetCurrent方法()。ToString()+ ex.StackTrace);

}

返回retUserInsert;



/ / click事件在这个事件中可以添加新用户



protected void btnRegister_Click(object sender,EventArgs e)

{

尝试

{

lblMsg.Text =;

if(Request.QueryString [UserID] == null)

{



objBusinessUI.FirstName = txtFirstName.Text.Trim();

objBusinessUI.LastName = txtlastName.Text.Trim();

objBusinessUI.UserName = txtUserName.Text.Trim();

objBusinessUI.Email = txtEmail.Text.Trim();

objBusinessUI.Password = txtPass.Text.Trim();

objBusinessUI.LastLogIn = DateTime.Now;

// Ob jbussinessRegistration.CreatedDate = DateTime.Now;

//ObjbussinessRegistration.LastLoginDate = DateTime.Now;

objBusinessUI.Active =False;

objBusinessUI.CreatedOn = DateTime.Now;



System.Guid U_id = System.Guid.NewGuid();

objBusinessUI.UserID = Convert .ToInt32(U_id.ToString());



string strMsg = objDataUserUI.InsertUsers(objBusinessUI);



}

}

catch(例外情况)

{



log.Error(System.Reflection.MethodBase.GetCurrentMethod()。ToString()+ ex.StackTrace);

}







我在它上面设置调试器。

它抛出异常,如



捕获格式异常
输入字符串格式不正确。



任何人都可以帮助我

Thnxx

解决方案



hi guyss
I am getting error in this code.
I am trying to do I am generating auto-incre user id by using
System.Guid U_id = System.Guid.NewGuid(); systems class

System.Guid U_id = System.Guid.NewGuid();
 bjBusinessUI.UserID = Convert.ToInt32(U_id.ToString());
 string strMsg = objDataUserUI.InsertUsers(objBusinessUI);.//This is method 



//Method
public string InsertUsers(Business.Users objUsers)
{
string retUserInsert = string.Empty;
try
{
// string conneString = "Data Source=.;Initial Catalog=Vestigo;Integrated Security=True";
using (SqlConnection conn = Connection.OpenConnection())
{
// conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT_USER";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Connection = conn;
SqlParameter[] sqlParams = new SqlParameter[15];

SqlParameter UserName = new SqlParameter("@UserName", objUsers.UserName);
UserName.Direction = System.Data.ParameterDirection.Input;
UserName.DbType = System.Data.DbType.String;
sqlParams[0] = UserName;

SqlParameter FirstName = new SqlParameter("@FirstName", objUsers.FirstName);
FirstName.Direction = System.Data.ParameterDirection.Input;
FirstName.DbType = System.Data.DbType.String;
sqlParams[1] = FirstName;

SqlParameter LastName = new SqlParameter("@LastName", objUsers.LastName);
LastName.Direction = System.Data.ParameterDirection.Input;
LastName.DbType = System.Data.DbType.String;
sqlParams[2] = LastName;

SqlParameter Password = new SqlParameter("@Password", objUsers.Password);
Password.Direction = System.Data.ParameterDirection.Input;
Password.DbType = System.Data.DbType.DateTime;
sqlParams[3] = Password;

SqlParameter Email = new SqlParameter("@Email", objUsers.Email);
Email.Direction = System.Data.ParameterDirection.Input;
Email.DbType = System.Data.DbType.String;
sqlParams[4] = Email;

SqlParameter LastLogIn = new SqlParameter("@LastLogIn", objUsers.LastLogIn);
LastLogIn.Direction = System.Data.ParameterDirection.Input;
LastLogIn.DbType = System.Data.DbType.DateTime;
sqlParams[5] = LastLogIn;

SqlParameter CreatedBy = new SqlParameter("@CreatedBy", objUsers.CreatedBy);
CreatedBy.Direction = System.Data.ParameterDirection.Input;
CreatedBy.DbType = System.Data.DbType.String;
sqlParams[6] = CreatedBy;

SqlParameter CreatedOn = new SqlParameter("@CreatedOn", objUsers.CreatedOn);
CreatedOn.Direction = System.Data.ParameterDirection.Input;
CreatedOn.DbType = System.Data.DbType.DateTime;
sqlParams[7] = CreatedOn;

SqlParameter UpdatedBy = new SqlParameter("@UpdatedBy", objUsers.UpdatedBy);
CreatedOn.Direction = System.Data.ParameterDirection.Input;
CreatedOn.DbType = System.Data.DbType.String;
sqlParams[8] = UpdatedBy;

SqlParameter UpdatedOn = new SqlParameter("@UpdatedOn", objUsers.UpdatedOn);
UpdatedOn.Direction = System.Data.ParameterDirection.Input;
UpdatedOn.DbType = System.Data.DbType.DateTime;
sqlParams[9] = UpdatedOn;

SqlParameter Active = new SqlParameter("@Active", objUsers.Active);
Active.Direction = System.Data.ParameterDirection.Input;
Active.DbType = System.Data.DbType.Boolean;
sqlParams[10] = Active;


//SqlParameter SecurityQuestion = new SqlParameter("@SecurityQuestion", objUsers.SecurityQuestion);
//SecurityQuestion.Direction = System.Data.ParameterDirection.Input;
//SecurityQuestion = System.Data.DbType.DateTime;
//sqlParams[4] = SecurityQuestion;

//SqlParameter Answer = new SqlParameter("@Answer", objUsers.Answer);
//Answer.Direction = System.Data.ParameterDirection.Input;
//Answer.DbType = = System.Data.DbType.DateTime;
//sqlParams[4] = Answer;


cmd.Parameters.AddRange(sqlParams);
cmd.ExecuteNonQuery();
//conn.Close();


}
}
catch (Exception ex)
{

log.Error(System.Reflection.MethodBase.GetCurrentMethod().ToString() + ex.StackTrace);
}
return retUserInsert;

//click event in this event new user is can be added

protected void btnRegister_Click(object sender, EventArgs e)
{
try
{
lblMsg.Text = "";
if (Request.QueryString["UserID"] == null)
{

objBusinessUI.FirstName = txtFirstName.Text.Trim();
objBusinessUI.LastName = txtlastName.Text.Trim();
objBusinessUI.UserName = txtUserName.Text.Trim();
objBusinessUI.Email = txtEmail.Text.Trim();
objBusinessUI.Password = txtPass.Text.Trim();
objBusinessUI.LastLogIn = DateTime.Now;
//ObjbussinessRegistration.CreatedDate = DateTime.Now;
//ObjbussinessRegistration.LastLoginDate = DateTime.Now;
objBusinessUI.Active = "False";
objBusinessUI.CreatedOn = DateTime.Now;

System.Guid U_id = System.Guid.NewGuid();
objBusinessUI.UserID = Convert.ToInt32(U_id.ToString());

string strMsg = objDataUserUI.InsertUsers(objBusinessUI);

}
}
catch (Exception ex)
{

log.Error(System.Reflection.MethodBase.GetCurrentMethod().ToString() + ex.StackTrace);
}



while i set the debugger on it.
Its throws exception like

Format exception is caught
Input string was not in a correct format.

can anybody help me
Thnxx

解决方案




这篇关于自动生成用户ID时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:44