Hi i am a newbie to C# and are trying to create a inventory management system for my team.
I have created the form and i have tested that the data goes into the sql database table, however everytime i launch my form the dataGridView automatically adds a row.. Secondly i want my Wkst_Status field to allow ACTIVE to be true or visible and not have the default name select visible. hope you can help me in this regard.
Thanks in advance
Jason
see code below:
this.workstationsTableAdapter.Fill(this.stockInventoryDataSet.Workstations);
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = (@"Data Source=SH-JASONK\DEV;Initial Catalog=StockInventory;Integrated Security=True");
con.Open();
bool status = false;
if (combostatus.SelectedIndex == 0)
{
status = true;
}
else
{
status = false;
}
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText =
cmd.CommandText = (@"INSERT INTO [StockInventory].[dbo].[Workstations](Emp_Name,
Emp_Surname,
Department,
Company,
Hostname,
Wkst_Status,
Make, Model,
SerialNumber,
ProductNumber,
PurchaseDate,
ExpiryDate,
Memory,
Processor,
HDD,
OS,
MSOffice)
VALUES ('" + txtname.Text + "',
'" + txtsurname.Text + "',
'" + combodept.Text + "',
'" + combocompany.Text + "',
'" + txthostname.Text + "',
'" + combostatus.Text + "',
'" + combomake.Text + "',
'" + txtmodel.Text + "',
'" + textsn.Text + "',
'" + txtprodnum.Text + "',
'" + dateTimePicker1.Value.ToString("yyyy/MM/dd") + "',
'" + dateTimePicker2.Value.ToString("yyyy/MM/dd") + "',
'" + combomem.Text + "',
'" + txtproc.Text + "',
'" + combohdd.Text + "',
'" + comboOS.Text + "',
'" + combooffice.Text + "')");
cmd.ExecuteNonQuery();
con.Close();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM
[StockInventory].[dbo].[Workstations] ", con);
DataTable dt = new DataTable();
sda.Fill(dt);
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item["Emp_Name"].ToString();
dataGridView1.Rows[n].Cells[1].Value = item["Emp_Surname"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["Department"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["Company"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["Hostname"].ToString();
bool combostatus = true;
if (combostatus)
{
dataGridView1.Rows[n].Cells[5].Value = "ACTIVE";
}
else
{
dataGridView1.Rows[n].Cells[5].Value = "INACTIVE";
}
dataGridView1.Rows[n].Cells[5].Value = item["Make"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["Model"].ToString();
dataGridView1.Rows[n].Cells[7].Value = item["SerialNumber"].ToString();
dataGridView1.Rows[n].Cells[8].Value = item["ProductNumber"].ToString();
dataGridView1.Rows[n].Cells[9].Value = item["PurchaseDate"].ToString();
dataGridView1.Rows[n].Cells[10].Value = item["ExpiryDate"].ToString();
dataGridView1.Rows[n].Cells[11].Value = item["Memory"].ToString();
dataGridView1.Rows[n].Cells[12].Value = item["Processor"].ToString();
dataGridView1.Rows[n].Cells[13].Value = item["HDD"].ToString();
dataGridView1.Rows[n].Cells[14].Value = item["OS"].ToString();
dataGridView1.Rows[n].Cells[15].Value = item["MSOffice"].ToString();
}
}
}
}
What I have tried:
i have set the AllowUserToAddRows to False in the properties of the dataGridView