Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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
Posted
Updated 10-Sep-13 2:02am
v3
Comments
Dholakiya Ankit 10-Sep-13 7:55am    
what value u got in guid?

The problem seems to be inside the InsertUsers method.
It looks like you're trying to parse a string (to an Integer?)

put a break point on this line:
C#
string strMsg = objDataUserUI.InsertUsers(objBusinessUI);.//This is method 

and press F11 which means "Step Into" to see what happens inside the function InsertUsers()

What you already do to help us understand is to copy the code of that function and paste it in your question (by clicking "Improve Question")

Awaiting your reply to continue solution...

Cheers,
Edo
 
Share this answer
 
v4
Comments
indrajeet jadhav 10-Sep-13 7:49am    
Control is not passing to this
string strMsg = objDataUserUI.InsertUsers(objBusinessUI) statement
exception throws bjBusinessUI.UserID = Convert.ToInt32(U_id.ToString());
after this statement
Joezer BH 10-Sep-13 7:50am    
Exactly!

The problem is with the conversion of U_id.ToString(), it probably is input that cannot be converted to an Integer!

Try to see what the value of U_id is before you try to parse it, and you will see.
C#
objBusinessUI.UserID = Convert.ToInt32(U_id.ToString());

The above line will try to convert the auto-generated GUID to integer. But the GUID that you generate using:
C#
System.Guid U_id = System.Guid.NewGuid();
is alphanumeric. So it can "never" be converted into an integer. Either you will have to change your logic to pass the id as a string or to generate a random integer instead of a GUID. BTW, you can configure your database table to use identity property and it will generate the id automatically.
 
Share this answer
 
v2
SqlParameter[] sqlParams = new SqlParameter[15];


You Have pass only 11 Parameter And you have to use 15 parameter

so change line and try again

SqlParameter[] sqlParams = new SqlParameter[11];


Happy Coding :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900