private void button1_Click(object sender, EventArgs e) { string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\14383\Downloads\BookBizz\BookBizz\Database1.mdf;Integrated Security=True"; string fname = firstname.Text; string lname = lastname.Text; string mail = email.Text; Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); Match match = regex.Match(mail); if (!match.Success) { error.Visible = true; errorCont.Visible = true; error.Text = "Email is not valid"; } else { error.Visible = false; errorCont.Visible = false; using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); string query = "INSERT INTO AuthorTable VALUES " + "('" + fname + "', '" + lname + "', '" + mail + "');"; SqlCommand cmd = new SqlCommand(query, con); cmd.ExecuteReader(); this.Hide(); InventoryControllerDashboard dashboard = new InventoryControllerDashboard(); dashboard.Show(); } } }
private void createBook_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); int modified; string isbn = bookIsbn.Text; string title = bookTitle.Text; string unitPrice = bookUnitPrice.Text; string year = bookYear.Text; string qohand = qoh.Text; SqlConnection con = new SqlConnection(connectionString); string query = "INSERT INTO BooksTable output inserted.Id VALUES " + "('" + isbn + "', '" + title + "', '" + unitPrice + "', '" + year + "', '" + qohand + "');"; using (SqlCommand cmd = new SqlCommand(query, con)) { con.Open(); modified = (int)cmd.ExecuteScalar(); if (con.State == System.Data.ConnectionState.Open) { con.Close(); } } Console.WriteLine(modified); foreach (int index in authors.SelectedIndices) { string str = "(" + modified + "," + authorIdArray[index] + ")"; sb.Append(str); sb.Append(","); } sb.Remove(sb.Length - 1, 1); string query1 = "INSERT INTO BookAuth VALUES " + sb; using (SqlCommand cmd = new SqlCommand(query1, con)) { con.Open(); cmd.ExecuteReader(); if (con.State == System.Data.ConnectionState.Open) { con.Close(); } } this.Hide(); InventoryControllerDashboard dashboard = new InventoryControllerDashboard(); dashboard.Show(); }