public List <Author> searchAuthors(AuthorSearchSdi authorSearchSdi) { con = SqlServerConnection.getConnnection(); con.Open(); List <Author> authors = new List <Author>(); StringBuilder sql = new StringBuilder(); Dictionary <String, Object> param = new Dictionary <String, Object>(); sql.Append("select AuthorId," + " CONCAT(fName,' ',mName,' ',lName) as name," + " DOB from Author"); sql.Append(" where 1 = 1 "); if (authorSearchSdi.AuthorId != null && !"".Equals(authorSearchSdi.AuthorId)) { sql.Append(" and AuthorId = @authorid "); param.Add("authorid", authorSearchSdi.AuthorId); } if (authorSearchSdi.Fname != null && !"".Equals(authorSearchSdi.Fname)) { sql.Append(" and fName = @fname "); param.Add("fname", authorSearchSdi.Fname); } if (authorSearchSdi.Lname != null && !"".Equals(authorSearchSdi.Lname)) { sql.Append(" and lName = @lname "); param.Add("lname", authorSearchSdi.Lname); } if (authorSearchSdi.Mname != null && !"".Equals(authorSearchSdi.Mname)) { sql.Append(" and mName = @mname "); param.Add("mname", authorSearchSdi.Mname); } SqlCommand command = new SqlCommand(sql.ToString(), con); foreach (KeyValuePair <String, Object> item in param) { command.Parameters.AddWithValue(item.Key, item.Value); } SqlDataReader data = command.ExecuteReader(); while (data.Read()) { Author author = new Author(); author.AuthorId = data.GetString(0); author.Name = data.GetString(1); author.DOB1 = data.GetDateTime(2); authors.Add(author); } data.Close(); con.Close(); return(authors); }
private void search_Click(object sender, EventArgs e) { AuthorSearchSdi authorSearchSdi = new AuthorSearchSdi(); if ("".Equals(authorIdTextBox.Text.Trim())) { authorSearchSdi.AuthorId = null; } else { authorSearchSdi.AuthorId = authorIdTextBox.Text.Trim(); } if ("".Equals(fnameTextBox.Text.Trim())) { authorSearchSdi.Fname = null; } else { authorSearchSdi.Fname = fnameTextBox.Text.Trim(); } if ("".Equals(mnameTextBox.Text.Trim())) { authorSearchSdi.Mname = null; } else { authorSearchSdi.Mname = mnameTextBox.Text.Trim(); } if ("".Equals(lnameTextBox.Text.Trim())) { authorSearchSdi.Lname = null; } else { authorSearchSdi.Lname = lnameTextBox.Text.Trim(); } List <Author> authors = authorService.searchAuthors(authorSearchSdi); AuthorSearchResult authorSearchResult = new AuthorSearchResult(); authorSearchResult.Authors = authors; authorSearchResult.ShowDialog(); }
public List <Author> searchAuthors(AuthorSearchSdi authorSearchSdi) { return(authorRepoService.searchAuthors(authorSearchSdi)); }