943,985 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 17397
  • C# RSS
Sep 4th, 2007
0

How to update a database?

Expand Post »
I'm new in using Visual C#. Can you give me bit of information as to how I can manage the data entered by a user using a form and store it in an access database?

In the application I have created, I planned of using OleDbCommand to manage the storage of data but what I usually receive is an error "An unhandled exception of type 'System.IndexOutOfRangeException' occured in system.data.dll"
"Additional Information: An OleDbParameter with ParameterName 'Check Voucher' is not contained by this OleDbParameterCollection."

This was a portion of my code.
C# Syntax (Toggle Plain Text)
  1. private void RetrieveLedgerInfo()
  2. {
  3. objLedgerInfo.Parameters["Check Voucher"].Value =
  4. txtCVNo.Text;
  5. objLedgerInfo.Parameters["Payee"].Value =
  6. txtPayee.Text;
  7. oleDbConnection1.Open();
  8. objLedgerInfo.ExecuteNonQuery();
  9. oleDbConnection1.Close();
  10. }
  11. private void menuItem2_Click(object sender, System.EventArgs e)
  12. {
  13. RetrieveLedgerInfo();
  14. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
ebabes is offline Offline
73 posts
since Sep 2007
Sep 4th, 2007
0

Re: How to update a database?

The class below is a proxy (or wrapper) for making oledb calls.
You can use std sql scripts.
Just make sure that you have the appropriate connection string for your db.

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data; // ado.net stuff
  5. using System.Data.OleDb;
  6.  
  7. namespace DbProxy
  8. {
  9.  
  10. public class OleDbProxy
  11. {
  12. private string dbConnString;
  13.  
  14. public OleDbProxy(string db_conn_str_)
  15. {
  16. dbConnString = db_conn_str_;
  17. }
  18.  
  19. /// <summary>
  20. /// purpose:
  21. /// - performs the sql command
  22. /// - eg CREATE, DROP, INSERT, UPDATE, ...
  23. /// requirements:
  24. /// - sql_ is a valid, sql cmd
  25. /// - the user has sufficient privileges to perform the operation
  26. /// promise:
  27. /// - returns the result of the cmd
  28. /// - result >= 0
  29. /// - if query_ is invalid, throws System.Exception
  30. /// - if user is invalid, throws System.Exception
  31. /// </summary>
  32. /// <param name="sql_">the non query sql command</param>
  33. /// <returns></returns>
  34. public virtual int NonQuery(string sql_)
  35. {
  36. int result = 0;
  37. OleDbConnection conn = null;
  38. try
  39. {
  40. conn = GetConnection();
  41. conn.Open();
  42. OleDbCommand cmd = new OleDbCommand(sql_, conn);
  43. result = cmd.ExecuteNonQuery();
  44. conn.Close();
  45. }
  46. catch (Exception ex_)
  47. {
  48. throw new Exception(ex_.Message);
  49. }
  50. finally
  51. {
  52. if (conn != null)
  53. {
  54. conn.Close();
  55. }
  56. }
  57. return Math.Abs(result);
  58. }
  59.  
  60. /// <summary>
  61. /// purpose:
  62. /// - performs the sql command
  63. /// - eg CREATE, DROP, INSERT, UPDATE, ...
  64. /// requirements:
  65. /// - sql_ is a valid, sql cmd
  66. /// - the user has sufficient privileges to perform the operation
  67. /// promise:
  68. /// - returns the result of the cmd
  69. /// - result >= 0
  70. /// - if query_ is invalid, throws System.Exception
  71. /// - if user is invalid, throws System.Exception
  72. /// </summary>
  73. /// <param name="sql_">the non query sql command</param>
  74. /// <returns></returns>
  75. public virtual object ExecScalar(string sql_)
  76. {
  77. object result = null;
  78. OleDbConnection conn = null;
  79. try
  80. {
  81. conn = GetConnection();
  82. conn.Open();
  83. OleDbCommand cmd = new OleDbCommand(sql_, conn);
  84. result = cmd.ExecuteScalar();
  85. conn.Close();
  86. }
  87. catch (Exception ex_)
  88. {
  89. throw new Exception(ex_.Message);
  90. }
  91. finally
  92. {
  93. if (conn != null)
  94. {
  95. conn.Close();
  96. }
  97. }
  98. return result;
  99. }
  100.  
  101. /// <summary>
  102. /// purpose:
  103. /// - performs the sql query 'query_'
  104. /// requirements:
  105. /// - query_ is a valid, readonly query
  106. /// - the user has sufficient privileges to perform the operation
  107. /// promise:
  108. /// - returns the result of the query
  109. /// - if query_ is invalid, throws System.Exception
  110. /// - if user is invalid, throws System.Exception
  111. /// </summary>
  112. /// <param name="query_"></param>
  113. /// <returns></returns>
  114. public virtual DataSet ReadOnlyQuery(string query_)
  115. {
  116. bool read_query = false;
  117. string write_query_type = "none";
  118. try
  119. {
  120. string query2 = query_.ToLower().TrimStart();
  121. int select_pos = query2.IndexOf("select");
  122. if (select_pos >= 0)
  123. {
  124. read_query = true;
  125. int insert_pos = query2.IndexOf("insert");
  126. int update_pos = query2.IndexOf("update");
  127. int delete_pos = query2.IndexOf("delete");
  128.  
  129. if (insert_pos > 0 && select_pos > insert_pos)
  130. {
  131. write_query_type = "insert";
  132. read_query = false;
  133. }
  134. if (update_pos > 0 && select_pos > update_pos)
  135. {
  136. write_query_type = "update";
  137. read_query = false;
  138. }
  139. if (delete_pos > 0 && select_pos > delete_pos)
  140. {
  141. write_query_type = "delete";
  142. read_query = false;
  143. }
  144. }
  145.  
  146. if (read_query)
  147. {
  148. return Query(query_);
  149. }
  150. else
  151. {
  152. throw new Exception("Only Read queries are allowed: " + write_query_type);
  153. }
  154. }
  155. catch (Exception ex_)
  156. {
  157. //throw new Exception(ex_.Message);
  158. throw ex_;
  159. }
  160. }
  161.  
  162. /// <summary>
  163. /// purpose:
  164. /// - performs the sql query 'query_'
  165. /// requirements:
  166. /// - query_ is a valid sql query
  167. /// - the user has sufficient privileges to perform the operation
  168. /// promise:
  169. /// - returns the result of the query
  170. /// - if query_ is invalid, throws System.Exception
  171. /// - if user is invalid, throws System.Exception
  172. /// </summary>
  173. /// <param name="query_"></param>
  174. /// <returns></returns>
  175. public DataSet Query(string query_)
  176. {
  177. OleDbConnection conn = null;
  178. DataSet ds = new DataSet("QueryResults");
  179. try
  180. {
  181. conn = GetConnection();
  182. OleDbDataAdapter adapter = new OleDbDataAdapter(query_, conn);
  183. adapter.FillSchema(ds, SchemaType.Mapped, ResultTableName);
  184. rowsReturned = adapter.Fill(ds, ResultTableName);
  185. }
  186. catch (Exception ex_)
  187. {
  188. throw new Exception(ex_.Message);
  189. }
  190. finally
  191. {
  192. if (conn != null)
  193. {
  194. conn.Close();
  195. }
  196. }
  197. return ds;
  198. }
  199.  
  200. /// <summary>
  201. /// purpose:
  202. /// - updates the db from the contents of dt_
  203. /// requirements:
  204. /// - query_ is a valid sql query which for the db table coresponding to dt_
  205. /// - eg:
  206. /// - dt_ has name "Orders" and contains columns OrdNum, ClientId, Total etc
  207. /// - query == "SELECT OrdNum, ClientId, Total FROM Orders"
  208. /// - are able to use a 'CommandBuilder' to create the insert, update & delete commands
  209. /// - the user has sufficient privileges to perform the operation
  210. /// promise:
  211. /// - returns the no. of rows updated in the db
  212. /// - if query_ is invalid, throws System.Exception
  213. /// - if user is invalid, throws System.Exception
  214. /// </summary>
  215. /// <param name="query_"></param>
  216. /// <param name="dt_"></param>
  217. /// <returns></returns>
  218. public int Update(string query_, DataTable dt_)
  219. {
  220. int rows = 0;
  221. OleDbConnection conn = null;
  222. try
  223. {
  224. conn = GetConnection();
  225. OleDbDataAdapter adapter = new OleDbDataAdapter(query_, conn);
  226. OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
  227. rows = adapter.Update(dt_);
  228. }
  229. catch (Exception ex_)
  230. {
  231. throw new Exception(ex_.Message);
  232. }
  233. finally
  234. {
  235. if (conn != null)
  236. {
  237. conn.Close();
  238. }
  239. }
  240. return rows;
  241. }
  242.  
  243. /// <summary>
  244. /// returns a new connection to the underlying db
  245. /// user is responsible for opening & closing the connection
  246. /// </summary>
  247. /// <returns></returns>
  248. public OleDbConnection GetConnection()
  249. {
  250. OleDbConnection db_connection = null;
  251. try
  252. {
  253. db_connection = new System.Data.OleDb.OleDbConnection(dbConnString);
  254. }
  255. catch (Exception)
  256. {
  257. }
  258. return db_connection;
  259. }
  260.  
  261.  
  262.  
  263. /// <summary>
  264. /// returns the name that the 'result table' will have
  265. /// ie the only table in the data set returned by a call to one of the query fns
  266. /// </summary>
  267. public string ResultTableName
  268. {
  269. get
  270. {
  271. return resultTablename;
  272. }
  273. }
  274.  
  275. private string resultTablename = "ResultSet";
  276.  
  277. /// <summary>
  278. /// no. of rows returned in last valid query
  279. /// </summary>
  280. /// <returns></returns>
  281. public string RowCount()
  282. {
  283. return rowsReturned.ToString();
  284. }
  285.  
  286. private int rowsReturned = 0;
  287. }
  288.  
  289. }
Reputation Points: 10
Solved Threads: 2
Newbie Poster
kon_t is offline Offline
17 posts
since Feb 2005
Sep 6th, 2007
0

Re: How to update a database?

Thanks a lot. Can you send me some application in Visual C# that involves data storage and retrieval to and from an Access database.

I would rather clearly understand if I see some applications so that I could internalize how these stuff works.

Thanks a lot...
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
ebabes is offline Offline
73 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Need to create a Threaded IP Listener, where to start?
Next Thread in C# Forum Timeline: how to call C# method in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC