public static bool UserInputDataExists(TextBox textBox) { SqlConnection conn = TravelExpertConnection.GetConnection(); string selectQuery = "SELECT COUNT (*) FROM Suppliers " + "WHERE SupName = '" + textBox + "'"; SqlCommand dbCommand = new SqlCommand(selectQuery, conn); dbCommand.Connection = conn; conn.Open(); int matchesCount = Convert.ToInt32(dbCommand.ExecuteScalar()); //int matchesCount = int.Parse(dbCommand.ExecuteScalar().ToString()); conn.Close(); return(matchesCount != 0); }
private void LoadComboAndGrid() { SqlConnection connection = TravelExpertConnection.GetConnection(); // we can get the Supname.. string query = "SELECT SupplierId, SupName from Suppliers order by SupName"; SqlDataAdapter supp = new SqlDataAdapter(query, connection); SqlCommand cmd = new SqlCommand(query, connection); DataTable tab = new DataTable(); supp.Fill(tab); cboSupName.DataSource = tab; cboSupName.DisplayMember = "SupName"; cboSupName.ValueMember = "SupplierId"; this.cboSupName.SelectedIndex = -1; //cmd.ExecuteNonQuery(); suppliersDataGridView.DataSource = SuppliersDB.GetAllSuppliers(); }
private void btnAddUpdate_Click(object sender, EventArgs e) { { using (SqlConnection connection = TravelExpertConnection.GetConnection()) { if (Validator.IsPresent(txtSupName)) { connection.Open(); SqlCommand cmd = connection.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "update Suppliers set SupName='" + txtSupName.Text + "'" + "where SupplierId='" + cboSupName.SelectedValue + "'"; cmd.ExecuteNonQuery(); connection.Close(); } } suppliersDataGridView.DataSource = SuppliersDB.GetAllSuppliers(); LoadComboAndGrid(); txtSupName.Text = ""; } }
private void cboSupName_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection con = TravelExpertConnection.GetConnection(); string query = "select * from Suppliers where SupName = '" + cboSupName.Text + "'"; SqlCommand cmd = new SqlCommand(query, con); SqlDataReader dr; try { con.Open(); dr = cmd.ExecuteReader(); while (dr.Read()) { string supname = (string)dr["SupName"]; txtSupName.Text = supname; } //dr.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }