datatype conversion error
-
string Code = "select Code from ds1 "
string s;
using (SqlCommand command = new SqlCommand(Code, conn))
{s = (string)command.ExecuteScalar(); } string update = " update ds1 set name='asd' where Code='" + s + "'";
In above code give the datatype conversion error. e.g. :
Unable to cast object of type 'System.Int64' to type 'System.String'.
I declared code column as a bigint datatype.
-
string Code = "select Code from ds1 "
string s;
using (SqlCommand command = new SqlCommand(Code, conn))
{s = (string)command.ExecuteScalar(); } string update = " update ds1 set name='asd' where Code='" + s + "'";
In above code give the datatype conversion error. e.g. :
Unable to cast object of type 'System.Int64' to type 'System.String'.
I declared code column as a bigint datatype.
string update = " update ds1 set name='asd' where Code='" + s.ToString() + "'";
Parwej Ahamad g.parwez@gmail.com
-
string Code = "select Code from ds1 "
string s;
using (SqlCommand command = new SqlCommand(Code, conn))
{s = (string)command.ExecuteScalar(); } string update = " update ds1 set name='asd' where Code='" + s + "'";
In above code give the datatype conversion error. e.g. :
Unable to cast object of type 'System.Int64' to type 'System.String'.
I declared code column as a bigint datatype.
What Datatype is the Field "Code" in the Database?
Vuyiswa Maseko, Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding VB.NET/SQL7/2000/2005 http://vuyiswamb.007ihost.com http://Ecadre.007ihost.com vuyiswam@tshwane.gov.za
-
What Datatype is the Field "Code" in the Database?
Vuyiswa Maseko, Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding VB.NET/SQL7/2000/2005 http://vuyiswamb.007ihost.com http://Ecadre.007ihost.com vuyiswam@tshwane.gov.za
-
string update = " update ds1 set name='asd' where Code='" + s.ToString() + "'";
Parwej Ahamad g.parwez@gmail.com
-
string Code = "select Code from ds1 "
string s;
using (SqlCommand command = new SqlCommand(Code, conn))
{s = (string)command.ExecuteScalar(); } string update = " update ds1 set name='asd' where Code='" + s + "'";
In above code give the datatype conversion error. e.g. :
Unable to cast object of type 'System.Int64' to type 'System.String'.
I declared code column as a bigint datatype.
The ExecuteScalar method returns an Int64 boxed in an object. You can only unbox this value as an Int64 value:
long code = (long)command.ExecuteScalar();
If you want this value as a string, you use the ToString method: s = code.ToString(); Now, to answer the next question that you would have asked: As the code field is a bigint, you should not use apostrophes around the value in the update query.Despite everything, the person most likely to be fooling you next is yourself.
-
then it Should look like this
string Code = "select Code from ds1 " ; **int** s; using (SqlCommand command = new SqlCommand(Code, conn)) { s = **(int)**command.ExecuteScalar(); } string update = " update ds1 set name='asd' where Code='" + s + "'";
Am not Sure what you are doing, but if i were to write your type of code, i would do it like this
public int GetData() { int Code; String strCon = "blablablablablablabla"; SqlConnection con = new SqlConnection(strCon); SqlCommand cmdselect = new SqlCommand(); cmdselect.CommandText = "select Code from ds1"; cmdselect.Connection = con; try { con.Open(); Code = (int)cmdselect.ExecuteNonQuery(); } catch(SqlException) { throw; } finally { con.Close(); } return Code; }
hope it Helps
Vuyiswa Maseko, Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding VB.NET/SQL7/2000/2005 http://vuyiswamb.007ihost.com http://Ecadre.007ihost.com vuyiswam@tshwane.gov.za
-
string Code = "select Code from ds1 "
string s;
using (SqlCommand command = new SqlCommand(Code, conn))
{s = (string)command.ExecuteScalar(); } string update = " update ds1 set name='asd' where Code='" + s + "'";
In above code give the datatype conversion error. e.g. :
Unable to cast object of type 'System.Int64' to type 'System.String'.
I declared code column as a bigint datatype.
Change the line s = (string)command.ExecuteScalar(); to s = command.ExecuteScalar().ToString();
-
The ExecuteScalar method returns an Int64 boxed in an object. You can only unbox this value as an Int64 value:
long code = (long)command.ExecuteScalar();
If you want this value as a string, you use the ToString method: s = code.ToString(); Now, to answer the next question that you would have asked: As the code field is a bigint, you should not use apostrophes around the value in the update query.Despite everything, the person most likely to be fooling you next is yourself.
Thanx a lot! my problem has been solved. Actually I want to discuss one more problem. I have a column which contains comlete path of a filename. My requirement is that i want to fetch only filename in running mode. I use this following query, but it is not give the output.
string code = "select * from path";
string s;
using (SqlCommand command = new SqlCommand(code, conn))
{
conn.Open();
SqlDataReader dr;
dr = command.ExecuteReader();
dr.Read();
string s1 = dr.GetValues(0);
s=s1
conn.Close();
}
string filename = Path.GetFileNameWithoutExtension(s);
SqlDataAdapter da = new SqlDataAdapter(filename, conn);
da.Fill(ds, "a");
DataList1.DataSource = ds.Tables["a"];
DataList1.DataBind();
}thanx again
-
Thanx a lot! my problem has been solved. Actually I want to discuss one more problem. I have a column which contains comlete path of a filename. My requirement is that i want to fetch only filename in running mode. I use this following query, but it is not give the output.
string code = "select * from path";
string s;
using (SqlCommand command = new SqlCommand(code, conn))
{
conn.Open();
SqlDataReader dr;
dr = command.ExecuteReader();
dr.Read();
string s1 = dr.GetValues(0);
s=s1
conn.Close();
}
string filename = Path.GetFileNameWithoutExtension(s);
SqlDataAdapter da = new SqlDataAdapter(filename, conn);
da.Fill(ds, "a");
DataList1.DataSource = ds.Tables["a"];
DataList1.DataBind();
}thanx again
ahmad25 wrote:
SqlDataAdapter da = new SqlDataAdapter(filename, conn);
The filename isn't an SQL query, is it? Also, the connection is closed. What is it that you are trying to do here really?
Despite everything, the person most likely to be fooling you next is yourself.
-
Change the line s = (string)command.ExecuteScalar(); to s = command.ExecuteScalar().ToString();
-
ahmad25 wrote:
SqlDataAdapter da = new SqlDataAdapter(filename, conn);
The filename isn't an SQL query, is it? Also, the connection is closed. What is it that you are trying to do here really?
Despite everything, the person most likely to be fooling you next is yourself.