Here's my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Store_Goods_List
{
public partial class Form1 : Form
{
SqlConnection cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\miki\documents\visual studio 2012\Projects\Store Goods List\Store Goods List\Database1.mdf;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cmd.Connection = cn;
loadlist();
}
private void label3_Click(object sender, EventArgs e)
{
if (txtnum.Text != "" & txtname.Text != "" & txtstock.Text != "" & txtprice.Text!="")
{
cn.Open();
cmd.CommandText = "Insert Info (product id,name,stock,price) values ('"+txtnum.Text+"','"+txtname.Text+"','"+txtstock.Text+"','"+txtprice.Text+"')";
cmd.ExecuteNonQuery();
cmd.Clone();
MessageBox.Show("Record inserted!","Programming");
cn.Close();
txtnum.Text = "";
txtname.Text = "";
txtstock.Text = "";
txtprice.Text = "";
loadlist();
}
}
private void loadlist()
{
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
listBox4.Items.Clear();
cn.Open();
cmd.CommandText = "select ' from info";
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
listBox1.Items.Add(dr[0].ToString());
listBox2.Items.Add(dr[1].ToString());
listBox3.Items.Add(dr[2].ToString());
listBox4.Items.Add(dr[3].ToString());
}
}
cn.Close();
}
private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox l = sender as ListBox;
if(l.SelectedIndex!= -1)
{
listBox1.SelectedIndex = l.SelectedIndex;
listBox2.SelectedIndex = l.SelectedIndex;
listBox3.SelectedIndex = l.SelectedIndex;
listBox4.SelectedIndex = l.SelectedIndex;
}
}
}
}
No comments:
Post a Comment