thatthing
thatthing

Reputation: 676

Char to Int in asp.net

I have a column in my database with the name: product_no and its data type is char(25)

i want to be able to search the name of the products by simply typing product_no in the search box and retrieve the result.

I am trying the code below but it doesnt work. When i replace product_no with some other columns in my database whose data types are int, the code below works.

I suspect that this code doesnt work because product_no is not int. Any ideas?

public string getproductdetail(int ID)
{
    Product prod = new Product();
    string connectionString =
      "server=server;uid=user;pwd=pwd;database=DB;";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        string sql = "Select name from dbo.product(nolock) Where product_no = " + ID.ToString();
        connection.Open();
  SqlCommand command = new SqlCommand(sql, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            prod.name          = reader["name"].ToString();
        }
    }
}
}
[DataContract]
public class Product
{
[DataMember]
public string name;
}

Upvotes: 0

Views: 276

Answers (3)

kabijoy
kabijoy

Reputation: 303

TRy Like This:

public string getproductdetail(int ID)
    {
        Product prod = new Product();
    String product_no=Convert.ToString(Id);
        string connectionString =
          "server=server;uid=user;pwd=pwd;database=DB;";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            string sql = "Select name from dbo.product(nolock) Where product_no = " + product_no.ToString();
            connection.Open();
      SqlCommand command = new SqlCommand(sql, connection);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                prod.name          = reader["name"].ToString();
            }
        }
    }
    }
    [DataContract]
    public class Product
    {
    [DataMember]
    public string name;
    }

Upvotes: 1

yogi
yogi

Reputation: 19591

There's no need for it until you are using = operator but if your want to use between, >, < etc. then you should convert that Char to an Int datatype. Other than that if you want to order the records in that column than too you should convert it.

To convert datatype of values in an SQL query you can try this

string sql = 
    "Select name from dbo.product(nolock) Where CONVERT(int, product_no) = @product_no";

for a proper documentation visit this.

Upvotes: 1

OriolBG
OriolBG

Reputation: 2037

If product_no is not an int type you should use ' characters in the query to enter the parameter. I recommend to use SqlParameter

using (SqlConnection connection = new SqlConnection(connectionString))
    {
        string sql = "Select name from dbo.product(nolock) Where product_no = @product_no";
        connection.Open();
        SqlCommand command = new SqlCommand(sql, connection);
        command.Parameters.Add(new SqlParameter("@product_no", ID);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            prod.name = reader["name"].ToString();
        }
    }

Upvotes: 1

Related Questions