3 Replies - 772 Views - Last Post: 24 December 2014 - 07:31 PM Rate Topic: -----

#1 Rabastan   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 141
  • Joined: 27-August 12

Display Data in text box based on ID

Posted 23 December 2014 - 10:24 PM

I have a area in my Project where i need to display a title and content in two text boxes. The Data base tables are:

ID | Title | Content

This is what I have so far, I know I am on the right track, I just cant figure out how to get each box to display based on the ID.
        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                string connStr = ConfigurationManager.ConnectionStrings["sdcAssistDB"].ConnectionString;
                OleDbConnection dbConn = new OleDbConnection(connStr);
                dbConn.Open();
                OleDbCommand dbCommand = new OleDbCommand();
                dbCommand.Connection = dbConn;
                string query = "select * from JobAid";
                dbCommand.CommandText = query;
                OleDbDataReader dbReader = dbCommand.ExecuteReader();
                while (dbReader.Read())
                {
                    txtTitle1.Text = (dbReader["Title"].ToString());
                    txtContent1.Text = (dbReader["Content"].ToString());
                    txtTitle2.Text = (dbReader["Title"].ToString());
                    txtContent2.Text = (dbReader["Content"].ToString());
                }
                dbConn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to Connect to Database" + Environment.NewLine + "SDC Assistant will now Close " +ex,
                    "Critical Database Error!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                    Application.Exit();
            }
        }




Thank You in advance for any help

Is This A Good Question/Topic? 0
  • +

Replies To: Display Data in text box based on ID

#2 Psyguy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 366
  • Joined: 12-January 11

Re: Display Data in text box based on ID

Posted 24 December 2014 - 12:36 PM

First, the database tables are collections of records. Each record has one or more fields. The table name is "JobAid". The fields for your table are ID, Title, and Content.

Second, a little more information is needed. What line of code are you experiencing problems on? Have you stepped through the code while executing and verified that the information you are requesting is being retrieved? Is your reader actually getting any records?

Answering these questions can help you nail down where the problem actually is.
Was This Post Helpful? 0
  • +
  • -

#3 ari03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 17-November 12

Re: Display Data in text box based on ID

Posted 24 December 2014 - 01:48 PM

im working on project may be this example be helpful for you as i hope
 SqlCommand cmd = new SqlCommand("select name,pic from tbimage where ID like('"+textBox1.Text.ToString()+ "%')", cn);
            SqlDataReader dr;
            cn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read()) 
            {
                string smd = (string)dr["name"];//in here you write the cell you want applay
                label1.Text = smd;// and here you put that variable 
//or you can use like textbox1.text=smd;
                byte[] imagg = (byte[])(dr["pic"]);
                if (imagg == null)
                    pictureBox1.Image = null;
                {
                    MemoryStream stream = new MemoryStream(imagg);
                    pictureBox1.Image = System.Drawing.Image.FromStream(stream);
                }


This post has been edited by ari03: 24 December 2014 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7447
  • View blog
  • Posts: 25,085
  • Joined: 05-May 12

Re: Display Data in text box based on ID

Posted 24 December 2014 - 07:31 PM

So why not make your query in the first post limit the item selected based on ID? You obviously knew how to do that in your second post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1