Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Hello everyone,

Im trying to fill some textboxes with values from a database table.when i write a name in a textbox,i use it as a parameter for the query,the query result should show the values in different textboxes,for example the surname in one textbox,the id in another etc.Is there any way to do this??

im not english,so i hope u understand me.

here is part of my code,the part where i check if enter is pressed,connect to the db,below are 3 ways for datareader but none of them shows anything:


C#
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            string _connString = String.Format(System.Globalization.CultureInfo.InvariantCulture,
            @"Data Source = {0}\SQL_CE.sdf", Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase));
            SqlCeConnection _connection = new SqlCeConnection(_connString);

            

            if (e.KeyChar == (char)13)
            {

                SqlCeCommand command = new SqlCeCommand("SELECT surname, ID FROM Data where name=@name;", _connection);
                
                command.Parameters.Add("@name", txt_name.Text);

                _connection.Open();
                SqlCeDataReader rdr = command.ExecuteReader();
                    
                if(rdr.Read())
                {
     
                    txt_surname.Text=Convert.IsDBNull(rdr["Surname"]) ? "" : Convert.ToString(rdr["Surname "]);
                    txt_ID.Text = rdr["ID"].ToString();

                    }
                    rdr.Close();

                }
Posted
Updated 13-Jul-17 5:42am
v2
Comments
Herman<T>.Instance 12-Mar-12 11:17am    
what have you tried? Please show your code and show where you are stuck. In that way we can help you.

This isn't the exact code, but it is a starting point. All the bits you need are there.
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT description FROM myTable WHERE ID=@ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", myId);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", myId, desc);
                }
            }
        }
    }
 
Share this answer
 
Comments
IviKAZAZI 12-Mar-12 11:29am    
i want to get multiple values,and set them in different textboxes
OriginalGriff 12-Mar-12 11:50am    
The code you have added shows you retrieving two items from a row and saving them to different TextBoxes.
However, I can't guarantee that it will fetch anything.
First off, don't use "magic numbers" like "(char)13" - use system names if possible. In this case it is: Keys.Return is the value you should be testing against.
Secondly, it depends where you have your event handler hooked, and where the user input cursor is.
If you are handling it for the form, then it will not occur when the cursor is in the TextBox unless the "KeyPreview" property of the Form is set to "true".
Have you tried putting a breakpoint on the _connString declaration line and stepping through your code?
Here is the sample code:

C#
this.textBoxName.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);


private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
 if (e.KeyChar == (char)13)
 {
   // Then Enter key was pressed
   string ConnectionString = "server=xxx;userid=aaa;"+ "pwd=bbb;database=northwind";
   con = new SqlConnection(ConnectionString);
   con.Open();
  string CommandText = "SELECT SurName" +
                       "  FROM Employees" +
                       " WHERE (Name LIKE @)" + textBoxName.Text;
  cmd = new SqlCommand(CommandText);
  rdr = cmd.ExecuteReader();
  while(rdr.Read())
  {
     textSurName = rdr["SurName"].ToString()
  }
 }
}
 
Share this answer
 
Comments
IviKAZAZI 12-Mar-12 11:34am    
i have tried this,but it doesnt show nothing,maby because my select statement select all colums of the table,because i need to read them all,and show them as text in the textboxes.

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