coversion
I have created a database & set userid as nvarchar & in aspx.cs file I have set id as int & want to convert int to nvarchar and insert in table. Help expected. Thank you
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Mar 15, 2015, 5:57 PM
userid = Int32.Parse(cmd.ExecuteScalar().ToString());
As it's a nvarchar in the database, is the userid actually numeric (e.g. 123) or alphanumeric (e.g. A123)?
If it's the latter, then you'll need need to strip away the alphabetic character(s) before you can parse what's left to an integer.
Anjali CPosted Mar 15, 2015, 2:44 AM
VulpesPosted Mar 14, 2015, 12:55 PM
Also. you don't actually need the DataAdapter here as your stored procedure is (presumably) returning a single value. So I'd try:
Anjali CPosted Mar 14, 2015, 12:33 PM
protected void Button1_Click(object sender, EventArgs e)
{
int userid = 0;
string str = ConfigurationManager.ConnectionStrings["GalleryString"].ConnectionString;
using (SqlConnection cn = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand("AddCustomer"))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FName", txtFName.Text);
cmd.Parameters.AddWithValue("@MName", txtMName.Text);
cmd.Parameters.AddWithValue("@LName", txtLName.Text);
cmd.Parameters.AddWithValue("@DOB", txtDOB.Text);
cmd.Parameters.AddWithValue("@MobileNo", txtMob.Text);
cmd.Parameters.AddWithValue("@Emailid", emailtxt.Text);
cmd.Parameters.AddWithValue("@gender", txtGender.Text);
cmd.Parameters.AddWithValue("@password", txtPswrd.Text);
cmd.Parameters.AddWithValue("@cnfswrd", txtVerify.Text);
cmd.Connection = cn;
cn.Open();
userid = Int32.Parse(cmd.ExecuteScalar());//I want the conversion here
cn.Close();
}
}
string msg = string.Empty;
switch (userid)
{
case -1: msg = "username exists";
break;
case -2: msg = "emailid has been used" + userid.ToString();
break;
default:
msg = "registration successfull" + userid.ToString();
break;
}
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + msg + "');", true);
}
}
}
Suraj SahooPosted Mar 14, 2015, 10:16 AM
You can try the below format. But please if possible convert the userID type in the table to int, as the mismatch is against consistency. But still for the time being try with:-