Sql Database and Listbox

Trying to write the data from the database to list box

Page 1 of 1

3 Replies - 9382 Views - Last Post: 30 May 2010 - 12:07 PM Rate Topic: -----

#1 vadlak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-May 10

Sql Database and Listbox

Posted 30 May 2010 - 07:14 AM

Hello,

I am new to the forums but I was refered here to get some help. My problem is that i have a list text box and a database and I know how to insert my data into it. Which works perfectly fine, But when I have no idea where to start to get the list box to show the data that was inputed.

Here is my Insert Code

     
private void button1_Click_1(object sender, EventArgs e)
        {
            Form1.lb.Items.Add(textBox1.Text + "" + textBox2.Text);
            string connectionstring = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
            SqlConnection connection = new SqlConnection(connectionstring);

            string First = textBox1.Text;
            string Last = textBox2.Text;
            SqlCommand command = new SqlCommand();
            command.Connection = connection;

            command.CommandText = ("INSERT INTO [User] ([First Name],[Last Name]) VALUES (@First, @Last)");
            command.Parameters.AddWithValue("@First", First);
            command.Parameters.AddWithValue("@Last", Last);
            connection.Open();
            command.ExecuteNonQuery();
        }



This works fine.

So basically i have that you type in the text box and it inputs it into the database. But then i need, in Form 1 to automatically pull that information and make it an Item in the list. I have spent 2 hours already and have looked at tutorial around the web heck i even searched your site. Im new to Sql but not c#

Is This A Good Question/Topic? 0
  • +

Replies To: Sql Database and Listbox

#2 Searock  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 259
  • Joined: 15-September 09

Re: Sql Database and Listbox

Posted 30 May 2010 - 08:26 AM

So are the textbox and the listbox in the same form or are they in different forms ?

This post has been edited by Searock: 30 May 2010 - 08:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Sql Database and Listbox

Posted 30 May 2010 - 08:43 AM

private void button1_Click_1(object sender, EventArgs e)
        {
            Form1.lb.Items.Add(textBox1.Text + "" + textBox2.Text);
            string connectionstring = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
            SqlConnection connection = new SqlConnection(connectionstring);

            string First = textBox1.Text;
            string Last = textBox2.Text;
            SqlCommand command = new SqlCommand();
            command.Connection = connection;

            command.CommandText = ("INSERT INTO [User] ([First Name],[Last Name]) VALUES (@First, @Last)");
            command.Parameters.AddWithValue("@First", First);
            command.Parameters.AddWithValue("@Last", Last);
            connection.Open();
            command.ExecuteNonQuery();
            //Add this method to update the listBox 'lb' after it saves the information.
            PopulateMyListBox();
        }
public void PopulateMyListBox()
{
    //Retrieve the dataset from the database.
    //Call the databind method on the listbox.
    lb.DataSource = YourDataSet;
    lb.DataBind();   
}


Was This Post Helpful? 1
  • +
  • -

#4 vadlak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-May 10

Re: Sql Database and Listbox

Posted 30 May 2010 - 12:07 PM

That didnt work for me. It didnt find databind. So i used a datareader

 
            SqlCommand cmd = new SqlCommand("SELECT ID from [User]", connection);

            SqlDataReader rdr = null;
            // 2. Call Execute reader to get query results
            rdr = cmd.ExecuteReader();

            // print the CategoryName of each record
            while (rdr.Read())
            {
                Form1.lb.Items.Add(rdr[0]);
            }



The problem though is that it goes. 12 then if do it again i get 123 and then 1234 etc

This post has been edited by vadlak: 30 May 2010 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1