Use parameters and a data adapter instead, this way the library can format your query correctly data types and all, plus using the SqlDataAdapter you can dump the data into a return value like so:
-
public DataTable ReturnData()
-
{
-
string ConnectionString = "connection string goes here";
-
DataTable data = new DataTable();
-
-
using (SqlConnection conn = new SqlConnection(ConnectionString))
-
{
-
conn.Open();
-
SqlCommand cmd = new SqlCommand("select * from table1 where no = @p");
-
cmd.Parameters.AddWithValue("p", int32.parse(txtno.text ));
-
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
-
{
-
ad.Fill(data);
-
}
-
conn.Close();
-
}
-
-
return data;
-
}
-
Note that in your query you use
@p where as when you use the AddWithValue(string, object) function you drop the @ and only provide
p.
Using the Fill method also has the benefit that this function return's a DataTable that can instantly be used in your code, each of the columns in the table are named, so this would work for example:
-
-
DataTable result = ReturnData();
-
-
MessageBox.Show(result.Rows[0]["column_name"].ToString());
-
-
Hope this helps!
Aimee.