Insert into Access DataBase in .CS filehttps://forums.asp.net/t/1944064.aspx?Insert+into+Access+DataBase+in+CS+fileTue, 19 Nov 2013 16:56:29 -080019440645537695https://forums.asp.net/p/1944064/5537695.aspx?Insert+into+Access+DataBase+in+CS+fileInsert into Access DataBase in .CS file
<p>I want to capture users as they login and creat a record in an Access database. Since there is a .cs file associated with the Login page and I'm using a Master page for the Login page, I can't use the AccessDatasource1.Insert() on the Login page and I don't
know how to do it in the .cs file. &nbsp;Any ideas?</p>
<p>Ray</p>
2013-10-16T15:41:06-07:005537714https://forums.asp.net/p/1944064/5537714.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<p>Hi,</p>
<p>either make a new data source on your page.</p>
<p>or make AccessDatabaseSource1 to public and call&nbsp; it from child.</p>
<p>&nbsp;</p>
2013-10-16T16:27:07-07:005538496https://forums.asp.net/p/1944064/5538496.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<p>Rajat; Thank you for getting back to me. Could you elaborate on your recommendation to
<span style="font-family:'Verdana','sans-serif'; font-size:12pt">AccessDatabaseSource1 to public and call&nbsp; it from child</span>?</p>
<p>&nbsp;</p>
<p>Ray</p>
2013-10-17T10:35:53-07:005538663https://forums.asp.net/p/1944064/5538663.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<p>try this</p>
<p><a title="from SkyDrive" href="http://sdrv.ms/TcEked" target="_blank"><strong>Donwload Solution</strong></a></p>
<p>&nbsp;</p>
<pre class="prettyprint">//Target Database : Ms-Access Database
string strAccessConnectionString =
@&quot;Provider=Microsoft.ACE.OLEDB.12.0;&quot; &#43;
@&quot;Data Source=C:\Database\TestDB.mdb;&quot;;
OleDbConnection accessConnection = new OleDbConnection(strAccessConnectionString);
accessConnection.Open();
OleDbCommand insertCommand = new OleDbCommand();
insertCommand.Connection = accessConnection;
insertCommand.CommandText = @&quot;INSERT INTO Cust (CustomerID, CustomerName, Country) VALUES (@id, @name, @country)&quot;;
foreach (DataRow row in dtCustomers.Rows)
{
Console.WriteLine(&quot;{0}\t{1}\t\t\b{2}&quot;, row[&quot;customerNumber&quot;].ToString(), row[&quot;customerName&quot;].ToString().PadRight(25), row[&quot;country&quot;].ToString());
insertCommand.Parameters.Clear();
insertCommand.Parameters.AddWithValue(&quot;@id&quot;, row[&quot;customerNumber&quot;].ToString());
insertCommand.Parameters.AddWithValue(&quot;@name&quot;, row[&quot;customerName&quot;].ToString());
insertCommand.Parameters.AddWithValue(&quot;@country&quot;, row[&quot;country&quot;].ToString());
insertCommand.ExecuteNonQuery();
}
accessConnection.Close();</pre>
<p></p>
2013-10-17T13:56:33-07:005543648https://forums.asp.net/p/1944064/5543648.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<p>Do I include this code in my Login.cs file? Will this add a row to an existing file?</p>
<p>&nbsp;</p>
<p>Ray</p>
2013-10-23T11:51:16-07:005544696https://forums.asp.net/p/1944064/5544696.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<p>It should (please check the url ), but for your particular scenario I would suggest this :</p>
<p>&nbsp;</p>
<p>Now as you can access database in the Ms-Access database file, may be you'd like to Have your database table looking like this ( in addition to all other columns you are using )</p>
<pre class="prettyprint">------------------------------------------------------------------------------------------------------
| <strong>user (string)</strong> | <strong>passwd (string)</strong> | <strong>active (bool)</strong> | <strong>last-login (date-time)</strong> | <strong>last-logout (date-time)</strong> |
------------------------------------------------------------------------------------------------------</pre>
<p>So instead performing insert operation&nbsp;in the database table everytime a user logs-in,&nbsp;you may&nbsp;want to simply update a column [ active to TRUE ]&nbsp;or&nbsp;if needed, a couple of columns [ active to true &amp; last-login to current System.DateTime.Now() ] wherever in
database&nbsp;you are storing your users. I'd suggest it as a better-practice to update the database instead inserting and deleting to keep track of active users.</p>
2013-10-24T10:29:27-07:005544724https://forums.asp.net/p/1944064/5544724.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<p>That's a great idea. Thanks, I will give it a try. Will changing from INSERT to UPDATE affect the code you proviously sent to me?</p>
<p>Ray</p>
2013-10-24T11:16:22-07:005544725https://forums.asp.net/p/1944064/5544725.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<p>Of course, you may need to change insert command into update command.&nbsp; The rest of the code would remain same for the most of the part</p>
<p>&nbsp;</p>
<p>Good Luck !</p>
2013-10-24T11:18:44-07:005560253https://forums.asp.net/p/1944064/5560253.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<p>I inserted the following code in the C# file</p>
<p>&nbsp;string strAccessConnectionString =<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @&quot;Provider=Microsoft.ACE.OLEDB.12.0;&quot; &#43;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @&quot;Data Source=\DataBase\Users.mdb;&quot;;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OleDbConnection accessConnection = new OleDbConnection(strAccessConnectionString);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; accessConnection.Open();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; accessConnection.Close();</p>
<p>And received the following error message when I tested it.</p>
<p><span color="#ff0000" style="color:#ff0000">&nbsp;OleDbConnection accessConnection = new OleDbConnection(strAccessConnectionString);
</span></p>
<p><span color="#ff0000" style="color:#ff0000">I know I didn't include all of the code you provided, but I wanted to see if I could make the connection to the database before I tried to modify it.<br>
</span></p>
2013-11-13T14:44:19-08:005560257https://forums.asp.net/p/1944064/5560257.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<p>I'm Sorry I didn't include the error message. Here it is.</p>
<p><strong>&nbsp;</strong><span face="Arial" style="font-family:Arial">CS0246: The type or namespace name 'OleDbConnection' could not be found (are you missing a using directive or an assembly reference?)</span><br>
<br>
</p>
<p></p>
2013-11-13T14:48:45-08:005564843https://forums.asp.net/p/1944064/5564843.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<pre class="prettyprint">using System.Data.OleDb;</pre>
<p>at the top.</p>
2013-11-19T16:01:48-08:005564860https://forums.asp.net/p/1944064/5564860.aspx?Re+Insert+into+Access+DataBase+in+CS+fileRe: Insert into Access DataBase in .CS file
<p>Thank you for your assistance. I will need to work on reading and updating the data base bnut I'm able to make the connection, open and close the data base.</p>
2013-11-19T16:56:29-08:00