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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Mar 14, 2015, 8:55 AM EDT
Also. you don't actually need the DataAdapter here as your stored procedure is (presumably) returning a single value. So I'd try:
Suraj SahooRank #382 · 6.6K reputationPosted Mar 14, 2015, 6:16 AM EDT
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:-
Anjali C64 reputationPosted Mar 14, 2015, 8:33 AM EDT
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);
}
}
}
Anjali C64 reputationPosted Mar 14, 2015, 10:44 PM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 15, 2015, 1:57 PM EDT
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.