Saturday, 28 September 2013

Combobox SelectedValue is not accepted, stubborn error

Combobox SelectedValue is not accepted, stubborn error

I have list of Employees table on SQL side, and on C# I have
Add/Update/Remove buttons.
I want to populate combobox with items AND with their values coming from
EmployeeID (SQL side), so I can update/delete them with
combobox.SelectedValue , no matter what the order in combobox is. However
it gives error all the time, and the error comes when I populate combobox
with text and value.
InvalidArgument=Value of '1' is not valid for 'index'.
This is the full code below. Please tell me codes to replace, and not
opinion or feedback.
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.IO;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string path;
SqlConnection cnn = new SqlConnection("Initial
Catalog=randomcompany;Data Source=localhost;Integrated
Security=SSPI;");
public Form1()
{
InitializeComponent();
}
void temizle()
{
textBox1.Text = "";
textBox2.Text = "";
pictureBox1.Image = null;
comboBox1.SelectedIndex = -1;
comboBox1.Items.Clear();
button1.Enabled = true;
button4.Enabled = false;
cnn.Open();
SqlCommand cmd = new SqlCommand("SELECT
EmployeeID,EmployeeFirstName,EmployeeLastName FROM Employees",
cnn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
comboBox1.Items.Insert(dr.GetInt32(0), dr.GetString(1)
+ dr.GetString(2) );
}
}
cnn.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
temizle();
}
private void button3_Click_2(object sender, EventArgs e)
{
temizle();
}
private void button2_Click(object sender, EventArgs e) //Browse
button
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Images
(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + "All files
(*.*)|*.*";
dlg.Title = "Select Employee Picture";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image =
System.Drawing.Image.FromFile(dlg.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
path = dlg.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e) //insert item
{
try
{
cnn.Open();
Byte[] imagedata = File.ReadAllBytes(path);
SqlCommand cmd = new SqlCommand("INSERT INTO Employees
(EmployeeFirstname, EmployeeLastname, EmployeePhoto)
VALUES (@item1,@item2,@img)", cnn);
cmd.Parameters.AddWithValue("@item1", textBox1.Text);
cmd.Parameters.AddWithValue("@item2", textBox2.Text);
cmd.Parameters.AddWithValue("@img", imagedata);
cmd.ExecuteNonQuery();
cnn.Close();
MessageBox.Show("Success!!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
temizle();
}
private void comboBox1_SelectedIndexChanged(object sender,
EventArgs e) //when combobox item is selected
{
button1.Enabled = false;
button4.Enabled = true;
try
{
cnn.Open();
SqlCommand cmd = new SqlCommand("SELECT
EmployeeFirstName,EmployeeLastName,EmployeePhoto FROM
Employees WHERE EmployeeID = @myvalue", cnn);
cmd.Parameters.AddWithValue("@myvalue",
(comboBox1.SelectedValue));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
textBox1.Text = dr.GetString(0);
textBox2.Text = dr.GetString(1);
byte[] img = (byte[])(dr[2]);
if (img == null)
{
pictureBox1.Image = null;
}
else
{
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = Image.FromStream(ms);
pictureBox1.SizeMode =
PictureBoxSizeMode.StretchImage;
}
}
}
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button4_Click(object sender, EventArgs e) //update item
{
try
{
cnn.Open();
SqlCommand cmd = new SqlCommand("UPDATE Employees SET
EmployeeFirstName = @item1,EmployeeLastName = @item2 WHERE
EmployeeID = @myvalue", cnn);
cmd.Parameters.AddWithValue("@myvalue",
comboBox1.SelectedValue);
cmd.Parameters.AddWithValue("@item1", textBox1.Text);
cmd.Parameters.AddWithValue("@item2", textBox2.Text);
cmd.ExecuteNonQuery();
cnn.Close();
MessageBox.Show("Successfully updated");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
temizle();
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button3_Click_1(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
}
}

No comments:

Post a Comment