Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
 

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 12-Mar-12 5:13am
IviKAZAZI1.4K
Edited 12-Mar-12 5:25am
v2
Comments
digimanus at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This isn't the exact code, but it is a starting point. All the bits you need are there.
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);
                }
            }
        }
    }
  Permalink  
Comments
IviKAZAZI at 12-Mar-12 11:29am
   
i want to get multiple values,and set them in different textboxes
OriginalGriff at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Here is the sample code:
 
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()
  }
 }
}
  Permalink  
Comments
IviKAZAZI at 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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 950
1 OriginalGriff 508
2 Abhinav S 233
3 thatraja 225
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,142
1 OriginalGriff 4,665
2 Peter Leow 3,774
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 12 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid