Guest User

Untitled

a guest
Jan 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. SqlConnection cn = new SqlConnection("Data Source = localhost; Integrated Security = True; Database = myDB;");
  2. SqlDataAdapter adp = new SqlDataAdapter();
  3.  
  4. private void LoadSearch()
  5. {
  6. switch (cmbCategory.Text)
  7. {
  8. case "All":
  9. adp.SelectCommand = new SqlCommand("SELECT * FROM tblCommunication WHERE LetterType LIKE '" + txSearch.Text.Trim() + "' OR LetterNumber LIKE '" + txSearch.Text.Trim() + "' OR LetterAmount LIKE '" + txSearch.Text.Trim() + "' OR LetterFrom LIKE '" + txSearch.Text.Trim() + "' OR LetterTo LIKE '" + txSearch.Text.Trim() + "' OR ReceivedBy LIKE '" + txSearch.Text.Trim() + "' OR Requisition LIKE '" + txSearch.Text.Trim() + "' OR LetterSubject LIKE '" + txSearch.Text.Trim() + "' OR LetterContent LIKE '" + txSearch.Text.Trim() + "' OR LetterRemarks LIKE '" + txSearch.Text.Trim() + "'", cn);
  10. DataTable dtAll = new DataTable();
  11. //cn.Open();
  12. adp.Fill(dtAll);
  13. dgCommunications.DataSource = dtAll;
  14. //cn.Close();
  15. break;
  16.  
  17. case "Incoming Communications":
  18. adp.SelectCommand = new SqlCommand("SELECT CommType = '" + cmbCategory.Text + "', LetterDate, LetterReceived, LetterType, LetterNumber, LetterAmount, LetterFrom, LetterTo, ReceivedBy, Requisition, LetterSubject, LetterContent, LetterRemarks FROM tblCommunication WHERE LetterType LIKE '" + txSearch.Text.Trim() + "' OR LetterNumber LIKE '" + txSearch.Text.Trim() + "' OR LetterAmount LIKE '" + txSearch.Text.Trim() + "' OR LetterFrom LIKE '" + txSearch.Text.Trim() + "' OR LetterTo LIKE '" + txSearch.Text.Trim() + "' OR ReceivedBy LIKE '" + txSearch.Text.Trim() + "' OR Requisition LIKE '" + txSearch.Text.Trim() + "' OR LetterSubject LIKE '" + txSearch.Text.Trim() + "' OR LetterContent LIKE '" + txSearch.Text.Trim() + "' OR LetterRemarks LIKE '" + txSearch.Text.Trim() + "'", cn);
  19. DataTable dtInc = new DataTable();
  20. // cn.Open();
  21. adp.Fill(dtInc);
  22. dgCommunications.DataSource = dtInc;
  23. //cn.Close();
  24. break;
  25.  
  26. case "Inside Communications":
  27. adp.SelectCommand = new SqlCommand("SELECT CommType = '" + cmbCategory.Text + "', LetterDate, LetterReceived, LetterType, LetterNumber, LetterAmount, LetterFrom, LetterTo, ReceivedBy, Requisition, LetterSubject, LetterContent, LetterRemarks FROM tblCommunication WHERE LetterType LIKE '" + txSearch.Text.Trim() + "' OR LetterNumber LIKE '" + txSearch.Text.Trim() + "' OR LetterAmount LIKE '" + txSearch.Text.Trim() + "' OR LetterFrom LIKE '" + txSearch.Text.Trim() + "' OR LetterTo LIKE '" + txSearch.Text.Trim() + "' OR ReceivedBy LIKE '" + txSearch.Text.Trim() + "' OR Requisition LIKE '" + txSearch.Text.Trim() + "' OR LetterSubject LIKE '" + txSearch.Text.Trim() + "' OR LetterContent LIKE '" + txSearch.Text.Trim() + "' OR LetterRemarks LIKE '" + txSearch.Text.Trim() + "'", cn); ;
  28. DataTable dtIns = new DataTable();
  29. //cn.Open();
  30. adp.Fill(dtIns);
  31. dgCommunications.DataSource = dtIns;
  32. //cn.Close();
  33. break;
  34.  
  35. case "Outgoing Communications":
  36. adp.SelectCommand = new SqlCommand("SELECT CommType = '" + cmbCategory.Text + "', LetterDate, LetterReceived, LetterType, LetterNumber, LetterAmount, LetterFrom, LetterTo, ReceivedBy, Requisition, LetterSubject, LetterContent, LetterRemarks FROM tblCommunication WHERE LetterType LIKE '" + txSearch.Text.Trim() + "' OR LetterNumber LIKE '" + txSearch.Text.Trim() + "' OR LetterAmount LIKE '" + txSearch.Text.Trim() + "' OR LetterFrom LIKE '" + txSearch.Text.Trim() + "' OR LetterTo LIKE '" + txSearch.Text.Trim() + "' OR ReceivedBy LIKE '" + txSearch.Text.Trim() + "' OR Requisition LIKE '" + txSearch.Text.Trim() + "' OR LetterSubject LIKE '" + txSearch.Text.Trim() + "' OR LetterContent LIKE '" + txSearch.Text.Trim() + "' OR LetterRemarks LIKE '" + txSearch.Text.Trim() + "'", cn); ;
  37. DataTable dtOut = new DataTable();
  38. //cn.Open();
  39. adp.Fill(dtOut);
  40. dgCommunications.DataSource = dtOut;
  41. // cn.Close();
  42. break;
  43. }
  44. }
  45.  
  46. using (SqlConnection connection = new SqlConnection(conString))
  47. {
  48. try
  49. {
  50. //your switch case statement
  51. }
  52. catch (InvalidOperationException)
  53. {
  54.  
  55. }
  56. catch (SqlException)
  57. {
  58.  
  59. }
  60. }
  61.  
  62. public void Command(string query,string conString)
  63. {
  64. SqlDataAdapter adp = new SqlDataAdapter();
  65. using (SqlConnection connection = new SqlConnection(conString))
  66. {
  67.  
  68. try
  69. {
  70. adp.SelectCommand = new SqlCommand(query,connection);
  71. DataTable dtOut = new DataTable();
  72. adp.Fill(dtOut);
  73. dgCommunications.DataSource = dtOut;
  74. }
  75. catch (InvalidOperationException)
  76. {
  77.  
  78. }
  79. catch (SqlException)
  80. {
  81.  
  82. }
  83. }
  84. }
  85.  
  86. SqlDataAdapter adp = new SqlDataAdapter();
  87. using (SqlConnection connection = new SqlConnection(conString))
  88. {
  89.  
  90. try
  91. {
  92. switch (cmbCategory.Text)
  93. {
  94. case "All":
  95. break;
  96.  
  97. }
  98. }
  99. catch (InvalidOperationException)
  100. {
  101.  
  102. }
  103. catch (SqlException)
  104. {
  105.  
  106. }
  107. }
  108.  
  109. class DAL
  110. {
  111. private readonly string _connectionString;
  112. public DAL(string connectionString)
  113. {
  114. _connectionString = connectionString;
  115. }
  116.  
  117. public DataTable SearchAll(string searchText)
  118. {
  119. var sql = "SELECT * FROM tblCommunication "+
  120. "WHERE LetterType = @searchText "+
  121. "OR LetterNumber = @searchText "+
  122. "OR LetterAmount = @searchText "+
  123. "OR LetterFrom = @searchText "+
  124. "OR LetterTo = @searchText "+
  125. "OR ReceivedBy = @searchText "+
  126. "OR Requisition = @searchText "+
  127. "OR LetterSubject = @searchText "+
  128. "OR LetterContent = @searchText "+
  129. "OR LetterRemarks = @searchText";
  130.  
  131. var parameter = new SqlParameter("@searchText", SqlDbType.VarChar);
  132. parameter.Value = searchText;
  133. return GetDataTable(sql, CommandType.Text, parameter);
  134. }
  135.  
  136. public DataTable SearchIncomingCommunications(string caterogy, string searchText)
  137. {
  138. // implementation same as the example above
  139. throw new NotImplementedException();
  140. }
  141.  
  142. public DataTable SearchInsideCommunications(string caterogy, string searchText)
  143. {
  144. // implementation same as the example above
  145. throw new NotImplementedException();
  146. }
  147.  
  148. // add other methods as well
  149.  
  150. private DataTable GetDataTable(string sql, CommandType commandType, params SqlParameter[] parameters)
  151. {
  152. var dt = new DataTable();
  153. using (var con = new SqlConnection(_connectionString))
  154. {
  155. using (var cmd = new SqlCommand(sql, con))
  156. {
  157. cmd.CommandType = commandType;
  158. foreach (var parameter in parameters)
  159. {
  160. cmd.Parameters.Add(parameter);
  161. }
  162. using (var da = new SqlDataAdapter(cmd))
  163. {
  164. da.Fill(dt);
  165. }
  166. }
  167. }
  168. return dt;
  169. }
  170. }
Add Comment
Please, Sign In to add comment