1

This is what my form looks like, and my code looks like this:

using System.Data.SqlClient;

namespace ProjectCSharpSQLserver
{
    public partial class Form1 : Form
    {
        SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=I:\ProjectCSharpSQLserver\ProjectCSharpSQLserver\CsSQL.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand();
        DataTable dt = new DataTable();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cn.Open();
            SqlDataAdapter sda = new SqlDataAdapter("insert into info (id, Name, phone, Address) Values ('" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "', '" + textBox4.Text + "')", cn);
            sda.Fill(dt);
            cn.Close();
        }
    }
}

I need to know how to open the combobox, find the Id, choose one and then then textboxes get populated by the data from the db, so I can delete and update data.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LMigo Max Pop
  • 43
  • 1
  • 1
  • 8
  • 1
    You would need to actually call the `combobox.SelectedItem` in your code, which you have yet to do. **Also**, passing a query directly to your `SqlDataAdapter` isn't a good practice. Learn about [SqlParameters](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx). – Brian Feb 19 '13 at 21:51
  • i've tried loop but i need a data count in a variable then i can add items into the combobox – LMigo Max Pop Feb 19 '13 at 21:55
  • and yeh iam a begainner and i just need an simple ways to do things – LMigo Max Pop Feb 19 '13 at 21:57
  • Please post all of your relevant code then. We here at SO are less interested in if you are a novice, intermediate or expert at coding, and much more interested in [what you have tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/) in learning to code :) – Brian Feb 19 '13 at 21:57
  • i think that's all, with that code i can insert data. thats all – LMigo Max Pop Feb 19 '13 at 22:00
  • So what the best resource to learn what i need , i do google every day but that makes me frustrated – LMigo Max Pop Feb 19 '13 at 22:04
  • earlier, you mentioned that you have a `combobox` on your main form. Where is the code for when you select the index of an item in the `combobox`? The hyperlink you provided for the screenshot of your form doesn't work. – Brian Feb 19 '13 at 22:22
  • no no no this is the problem i need that code, then i can do somthing like X = combobox.selecteditem.string(); and query like "DELETE FROM INFO WHERE ID LIKE '"+ X +"'"; – LMigo Max Pop Feb 19 '13 at 22:30

1 Answers1

1

I found an example that you can use here.

private void cb1_SelectedIndexChanged(object sender, EventArgs e)
{
   ComboBox cb = (ComboBox)sender;
   cn.Open();
   SqlDataAdapter sda = new SqlDataAdapter("Place your DELETE statement here", cn);
   sda.Fill(dt);
   cn.Close();
}

This would run the selected statement in the combobox each time it is changed. As an aside, when you run a SqlDataAdapter, it will automatically open and close a connection to the database for you so you don't have to use cn.Open()/cn.Close(). But doing so it still a good habit to get in to.

And just to remind you, I would seriously look into using parameterized queries once you are more comfortable with C# and ADO.NET.

Community
  • 1
  • 1
Brian
  • 5,069
  • 7
  • 37
  • 47