Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have developed and application.
their is an function to
fill all the textboxes from sql server database.
so sql database contains string values, int values, date values.

in application i have textboxes, datetimepickers, comboboxes.

C#
private void Search_Click(object sender, EventArgs e)
        {
            string myselection = ViewClass.SearchWrd;
            string connectionString = "Data Source=PD-JANAKAN;Initial Catalog=EnqInfo;Integrated Security=True";
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand("SELECT * FROM Login WHERE Id==@Id", connection);
            command.Parameters.AddWithValue("@Id", myselection);
            SqlDataReader reader = command.ExecuteReader();


            while (reader.Read())
            {
                string Cust_Name = reader.GetString(3);  // Name string
                if (reader["Cust_Cntact"] != DBNull.Value)
                {
                    int cust = Convert.ToInt32(reader["Cust_Cntact"]);
                }
                dateTimePickerJoinDate.Value = dt.Rows[0]["JoinDate"].ToString();}



can someone help me to load datetimepicker using sql date value from sql
and load textbox using database integer values?
and load comboboxes using database integer and string values?

i guess my code is wrong except get string values to textboxes and database int values to textboxes.

Thanks...

please help me to solve this. big help............
Posted

Try:
C#
dateTimePickerJoinDate.Value = (DateTime) reader["JoinDate"];
 
Share this answer
 
Few code improvements
1. Refer the documentation and study them, for example if you read SqlCommand.ExecuteReader Method[^] MSDN documentation page:
sample code:
C#
//using statement will dispose/close the connection automatically when it leave the block
using (SqlConnection connection = new SqlConnection(
           connectionString))
{
    // you haven't open connection in your code
    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
SQL
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
C#
if (reader.Read())
{
  CustNameTxt.Text = reader.GetString(3);
  // other field values......
}


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 :
C#
 CustNameTxt.Text = reader.GetString(3);
//above will work because GetString will return string value form database reader
 int age =25;
 TextBoxAge.Text = age.ToString();
// age is int value but Text property need string type, ToString method convert it to a string value

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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900