Few code improvements
1. Refer the documentation and study them, for example if you read
SqlCommand.ExecuteReader Method[
^] MSDN documentation page:
sample code:
using (SqlConnection connection = new SqlConnection(
connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(String.Format("{0}", reader[0]));
}
}
by studying above code you will learn best practices like "using" statement and make sure that you have open the connection before the ExecuteReader etc..
2. don't use double equal sign in sql where condition, it should be like below
SELECT * FROM Login WHERE Id=@Id
3. Do we really need while loop here? for given ID if only one record available you don't get multiple results from the sql statement and also if you have multiple records when you set value to a text box inside while loop it will re set every record which return from the database, at the end you will have last record data on the UI. So you can change the while loop to if condition if you need one record data as below
if (reader.Read())
{
CustNameTxt.Text = reader.GetString(3);
}
4. when you set value to a property of a control or some other field in your program you better check the type of the property\field is maching with the value you going to set. for example
TextBox Text Property need string type[
^] and you need to set value in string type. for example :
CustNameTxt.Text = reader.GetString(3);
int age =25;
TextBoxAge.Text = age.ToString();
then how you set datetime picket value? what is the type need for
datetime picker value property[
^]? how you read
datetime column value from sqlreader[
^]?
if you understand my explanation you will find answers to above questions and fix the issues in above code.
Good luck!