|
The error is on this line:
SetPreviousData2(registerNumber, bregisterNumber);
I don't know if it is because is it not being called by another method.
When I tested yesterday even before I added all the codes, I was able to go to the next page (Repeater2) and the two values for AregNo from MarineRegNo and coastGuardNumber from VesselRegNo were there.
Then that error started.
By the way, I did remove the if=== block you told me to remove yesterday.
It turned out that I was getting those errors from SetInitialRow2() because I forgot to remove that method and that method was calling SetPreviousData() methos.
|
|
|
|
|
samflex wrote: I don't know if it is because is it not being called by another method.
What do you mean by that?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
What I meant was that before, I was calling it inside AddNewRow2 method.
This error, I think started after I removed the AddNewRow2 method.
If I comment that line:
SetPreviousData2(registerNumber, bregisterNumber), then when click the NEXT button, the page is blank.
It may be that it is no longer needed but something is breaking the page.
|
|
|
|
|
Ok, I think the problem is here, not necessarily your problem but I may have missed something here because right now, when I click the NEXT page, it loads the form correctly except that the value of MarineRegNo and VesselRegNo in Repeater1 that were to populate AregNo and coastGuardNumber respectively, are not populating them.
Could it be in this method?
YEAR - FOUND IT!!
I had AregNo and coastGuardNumber twice, one empty and the other not.
NOW, when I enter values for Repeater1, they appear in Repeater2, YEAAA.
The real which I am about to try now is when I submit the data and try to populate the form with the account number, will the data I submitted load?
You are so gifted, I gush with jealousy.
private void PopulateRecord(DataTable table1)
{
DataTable table2 = LoadTable2(true);
table2.Rows.Clear();
foreach (DataRow row in table1.Rows)
{
DataRow dr = table2.NewRow();
dr["RowNumber"] = table2.Rows.Count + 1;
dr["aRegNo"] = row["MarineRegNo"];
dr["coastGuardNumber"] = row["VesselRegNo"];
dr["boatcnty"] = string.Empty;
dr["ARegNo"] = string.Empty;
dr["aMake"] = string.Empty;
dr["mMake"] = string.Empty;
dr["ModelNoA"] = string.Empty;
dr["ModelNoM"] = string.Empty;
dr["boatYrBuilt"] = string.Empty;
dr["motorYrBuilt"] = string.Empty;
dr["blength"] = string.Empty;
dr["hullmaterial"] = string.Empty;
dr["mhorsepower"] = string.Empty;
dr["rdlmhorsepType"] = string.Empty;
dr["datePurchased"] = string.Empty;
dr["mdatePurchased"] = string.Empty;
dr["PurchaseType"] = string.Empty;
dr["rblmPurchasedType"] = string.Empty;
dr["PrevOnwerName"] = string.Empty;
dr["prevAddr"] = string.Empty;
dr["prevCity"] = string.Empty;
dr["PrevState"] = string.Empty;
dr["prevzip"] = string.Empty;
dr["mPrevOnwerName"] = string.Empty;
dr["mprevAddr"] = string.Empty;
dr["mprevCity"] = string.Empty;
dr["mPrevState"] = string.Empty;
dr["mprevzip"] = string.Empty;
dr["buyername"] = string.Empty;
dr["buyeraddress"] = string.Empty;
dr["buyercity"] = string.Empty;
dr["buyerstate"] = string.Empty;
dr["buyerzip"] = string.Empty;
dr["datesold"] = string.Empty;
dr["salePrice"] = string.Empty;
dr["Description"] = string.Empty;
dr["mbuyername"] = string.Empty;
dr["mbuyeraddress"] = string.Empty;
dr["mbuyercity"] = string.Empty;
dr["mbuyerstate"] = string.Empty;
dr["mbuyerzip"] = string.Empty;
dr["mdatesold"] = string.Empty;
dr["msalePrice"] = string.Empty;
dr["mDescription"] = string.Empty;
dr["boatCost"] = string.Empty;
dr["motorCost"] = string.Empty;
dr["rblIssues"] = string.Empty;
dr["functionalIsses"] = string.Empty;
dr["mrblIssues"] = string.Empty;
dr["mfunctionalIsses"] = string.Empty;
dr["vesseltypeUse"] = string.Empty;
dr["vesselname"] = string.Empty;
dr["vessellength"] = string.Empty;
dr["vesselYrBult"] = string.Empty;
dr["VesselHullMateria"] = string.Empty;
dr["vesselhorsepEngine"] = string.Empty;
dr["coastGuardNumber"] = string.Empty;
dr["VesselYrPurchased"] = string.Empty;
dr["PurchasedNew"] = string.Empty;
dr["VesselPurchaseAmt"] = string.Empty;
dr["homePort"] = string.Empty;
dr["whereDocked"] = string.Empty;
dr["bmequipAccessoryList"] = string.Empty;
table2.Rows.Add(dr);
}
Repeater2.DataSource = table2;
Repeater2.DataBind();
foreach (RepeaterItem item in Repeater2.Items)
{
((Button)item.FindControl("BtnAdd2")).Visible = table2.Rows.Count > 1;
((Button)item.FindControl("btnDelete2")).Visible = table2.Rows.Count > 1;
}
}
|
|
|
|
|
I am sorry for the bad news sir but I am getting nullReference exception on the following markup:
<%foreach (System.Data.DataRow row in dtAirInfoTable.Rows)
{%>
<tr>
<td>
<%=row.ItemArray[1].ToString()%>
</td>
<td align="center">
<%=row.ItemArray[2].ToString()%>
</td>
</tr>
<tr>
<td>
<%=row.ItemArray[3].ToString()%>
</td>
<td align="center">
<%=row.ItemArray[4].ToString()%>
</td>
</tr>
<% } %>
I used to have this line:
ViewState["TempData"] = dtCurrentTable;
on the OnClickNext method but it has changed and that line is no longer there.
Then on another method, I would reference it thus:
public DataTable dtAirInfoTable;
lblTotalPrev.Text = hfvalue.Value;
if (ViewState["TempData"] != null)
{
dtAirInfoTable = (DataTable)ViewState["TempData"];
}
This would allow me to display the values from these:
TextBox tbboatregNum = (TextBox)item.FindControl("txtboatregNum");
TextBox tbPayerret = (TextBox)item.FindControl("txtPayerret");
TextBox tbCGvesselNum = (TextBox)item.FindControl("cgaurdNumber");
TextBox tbCGtaxpayerret = (TextBox)item.FindControl("cguardreturnedval");
DataRow row = dt.Rows[rowIndex];
row["MarineRegNo"] = tbboatregNum.Text;
row["TaxPyrRetdVal"] = tbPayerret.Text;
row["VesselRegNo"] = tbCGvesselNum.Text;
row["VesselTaxPyrRetdVal"] = tbCGtaxpayerret.Text;
and display their total in this line:
lblTotal.Text = hfvalue.Value;
|
|
|
|
|
If you haven't set the default values for the columns, then the value will be null . Calling .ToString() on a null value will throw a NullReferenceException . Either set the default values for the columns, or use Convert.ToString(row.ItemArray[1]) .
As far as I can see, "TempData" was only ever set to the first table; in which case, you can replace it with:
lblTotalPrev.Text = hfvalue.Value;
dtAirInfoTable = LoadTable1(false);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Everything looks great sir.
I am able to enter data on Repeater1 and those that need to appear in Repeater2 are appearing correctly.
The issue I have had in the past is that I would go through this process to insert records into the database successfully but when I try inserting another record, it is blind.
I am confident it is all fixed up now but I want to be sure when I go all the way to insert into the database but I am not able to do so because of an unrelated bug I am trying to resolve.
Once that is resolve, I will post a feedback.
Thanks so much Richard.
You have come through for me so many times. This is huge.
|
|
|
|
|
Hi Richard, well goodness, bad news scenario here.
I was able to successfully insert records into the database.
So, this is great. Thank you again.
Bad news is that when I attempt to populate the form with the records I inserted, I get the following:
Input string was not in a correct format.
That's because that field is empty:
dr["boatYrBuilt"] = string.Empty;
When I added value from the stored procedure I built to populate them shown below,
dr["boatYrBuilt"] = row["boatYrBuilt"];
dr["motorYrBuilt"] = row["Motor_Year_Built"];
I get Column 'boatYrBuilt' does not belong to table Table1.
To assign the values similar to this:
dr["coastGuardNumber"] = row["VesselRegNo"];
where does VesselRegNo come from?
I am sure the rest will give similar errors.
I was under the impression that as long the field name from the stored proc is assigned as Eval on Markup, this issue will not exist.
modified 22-Sep-17 15:48pm.
|
|
|
|
|
samflex wrote: Input string was not in a correct format.
On which line?
samflex wrote: Column 'boatYrBuilt' does not belong to table Table1
Which table are you updating? From what I can see, that should be the second table, and you should be creating the column in the LoadTable2 method.
samflex wrote: where does VesselRegNo come from?
dr is the new row in the second table; row is the row in the first table.
In the OnClickNext method, you copy the text from the cgaurdNumber control to the VesselRegNo column in the row from the first table.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
The error is coming from populateRecord method. I went back and looked at your solution, I believe I am using the code correctly. I am pasting here again just so you can take a look.
Sorry sir.
ivate void PopulateRecord(DataTable table1)
{
DataTable table2 = LoadTable2(true);
table2.Rows.Clear();
foreach (DataRow row in table1.Rows)
{
DataRow dr = table2.NewRow();
dr["RowNumber"] = table2.Rows.Count + 1;
dr["aRegNo"] = row["MarineRegNo"];
dr["coastGuardNumber"] = row["VesselRegNo"];
dr["boatcnty"] = string.Empty;
dr["aMake"] = string.Empty;
dr["mMake"] = string.Empty;
dr["ModelNoA"] = string.Empty;
dr["ModelNoM"] = string.Empty;
dr["boatYrBuilt"] = string.Empty;
dr["motorYrBuilt"] = string.Empty;
dr["blength"] = string.Empty;
dr["hullmaterial"] = string.Empty;
dr["mhorsepower"] = string.Empty;
dr["rdlmhorsepType"] = string.Empty;
dr["datePurchased"] = string.Empty;
dr["mdatePurchased"] = string.Empty;
dr["PurchaseType"] = string.Empty;
dr["rblmPurchasedType"] = string.Empty;
dr["PrevOnwerName"] = string.Empty;
dr["prevAddr"] = string.Empty;
dr["prevCity"] = string.Empty;
dr["PrevState"] = string.Empty;
dr["prevzip"] = string.Empty;
dr["mPrevOnwerName"] = string.Empty;
dr["mprevAddr"] = string.Empty;
dr["mprevCity"] = string.Empty;
dr["mPrevState"] = string.Empty;
dr["mprevzip"] = string.Empty;
dr["buyername"] = string.Empty;
dr["buyeraddress"] = string.Empty;
dr["buyercity"] = string.Empty;
dr["buyerstate"] = string.Empty;
dr["buyerzip"] = string.Empty;
dr["datesold"] = string.Empty;
dr["salePrice"] = string.Empty;
dr["Description"] = string.Empty;
dr["mbuyername"] = string.Empty;
dr["mbuyeraddress"] = string.Empty;
dr["mbuyercity"] = string.Empty;
dr["mbuyerstate"] = string.Empty;
dr["mbuyerzip"] = string.Empty;
dr["mdatesold"] = string.Empty;
dr["msalePrice"] = string.Empty;
dr["mDescription"] = string.Empty;
dr["boatCost"] = string.Empty;
dr["motorCost"] = string.Empty;
dr["rblIssues"] = string.Empty;
dr["functionalIsses"] = string.Empty;
dr["mrblIssues"] = string.Empty;
dr["mfunctionalIsses"] = string.Empty;
dr["vesseltypeUse"] = string.Empty;
dr["vesselname"] = string.Empty;
dr["vessellength"] = string.Empty;
dr["vesselYrBult"] = string.Empty;
dr["VesselHullMateria"] = string.Empty;
dr["vesselhorsepEngine"] = string.Empty;
dr["VesselYrPurchased"] = string.Empty;
dr["PurchasedNew"] = string.Empty;
dr["VesselPurchaseAmt"] = string.Empty;
dr["homePort"] = string.Empty;
dr["whereDocked"] = string.Empty;
dr["bmequipAccessoryList"] = string.Empty;
table2.Rows.Add(dr);
}
Repeater2.DataSource = table2;
Repeater2.DataBind();
foreach (RepeaterItem item in Repeater2.Items)
{
((Button)item.FindControl("BtnAdd2")).Visible = table2.Rows.Count > 1;
((Button)item.FindControl("btnDelete2")).Visible = table2.Rows.Count > 1;
}
}
|
|
|
|
|
There isn't anything in that which would throw an "Input string was not in a correct format" exception.
Are you sure it's not related to the markup for Repeater2 ?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Here is an example of how I am using these fields in Repeater2.
<td style="white-space: nowrap;">
Year Built:<asp:TextBox ID="boatYrBuilt" Text='<%#Eval("boatYrBuilt")%>' Style="width: 270px; margin-left: 250px;" runat="server"></asp:TextBox>
</td>
This happens only when I when I enter an account number to try to populate the form after tne records have been created.
The error does not occur when creating a new record, only when trying to pull an existing record.
|
|
|
|
|
Once again, there's nothing in the code you've shown which would produce the exception. You'll need to debug your code to find out where the exception is being thrown from.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Sorry to keep coming back sir.
Unless, I am not debugging this correctly, when I tried debugging, it keeps breaking on Repeater1 but shows boatYrBuilt as the culprit even though it is likely to not be the issue.
In other words, I enter account number, click enter and it loads page 1 which has personal info like name, phone, address, etc.
Then as soon as I click Next (to go to page2 which is Repeater1), it throws the incorrect format string error.
Could it be these?
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="txtboatregNum" Text='<%# Eval("MarineRegNo") %>' runat="server" placeholder="Enter registration #..." Style="width: 450px;" class="form-control"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtPayerret" Text='<%# Eval("TaxPyrRetdVal") %>' runat="server" placeholder="Enter Taxpayer returned value as of Jan. 1..." Style="width: 400px;" class="form-control txtPayerret"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="cgaurdNumber" Text='<%# Eval("VesselRegNo") %>' runat="server" placeholder="Enter Coast Guard #..." Style="width: 450px;" class="form-control"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="cguardreturnedval" Text='<%# Eval("VesselTaxPyrRetdVal") %>' runat="server" placeholder="Enter Taxpayer returned value as of Jan. 1..." Style="width: 400px;" class="form-control txtPayerret"></asp:TextBox>
</td>
<td>
<asp:Button ID="BtnAdd" Style="width: 150px;" runat="server" Text="Next Boat"
OnClick="BtnAdd_Click" CssClass="btnclass" />
<asp:Button ID="btnDelete" Style="width: 120px;" runat="server" Text="Remove a row"
OnClick="btnDelete_Click" CommandArgument='<%# Container.ItemIndex %>' CssClass="btnclass" />
</td>
</tr>
</ItemTemplate>
I am just grabbing at straws now.
|
|
|
|
|
The exception will include a stack trace. There may be some framework code at the top, but as you read down it, you should eventually find something that relates to your code.
The first item which refers to your code will tell you the file and the line which triggered the exception.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I know you have been very patient with me and you have given me more than I asked for (Thank you) but can you please look at the below code for me?
The problem I believe, is that when I enter an account number, it is not populating the form.
Could you please see if there is something to change with the code below?
TER PROCEDURE [dbo].[sp_AllRecs]
@pin int
AS
BEGIN
SET NOCOUNT ON;
SELECT ROW_Number() OVER (ORDER BY(SELECT 0)) RowNumber, mp.[DateSold] as datesold
,mp.[SalePrice] as salePrice
,mp.[Description] as Description
,mp.[BuyerFullName] as buyername
,mp.[BuyerAddress] as buyeraddress
,mp.[BuyerCity] as buyercity
,mp.[BuyerState] as buyerstate
,mp.[BuyerZipCode] as buyerzip
,mp.[mDateSold] as mdatesold
,mp.[mSalePrice] as msalePrice
,mp.[mDescription] as mDescription
,mp.[mBuyerFullName] as mbuyername
,mp.[mBuyerAddress] as mbuyeraddress
,mp.[mBuyerCity] as mbuyercity
,mp.[mBuyerState] as mbuyerstate
,mp.[mBuyerZipCode] as mbuyerzip
,mp.[PreviousOwnerName] as PrevOnwerName
,mp.[PreviousOwnerAddress] as prevAddr
,mp.[PreviousOwnerCity] as prevCity
,mp.[PreviousOwnerState] as PrevState
,mp.[PreviousOwnerZipCode] as prevzip
,mp.[mPreviousOwnerName] as mprevOnwerName
,mp.[mPreviousOwnerAddress] as mprevAddr
,mp.[mPreviousOwnerCity] as mprevCity
,mp.[mPreviousOwnerState] as mPrevState
,mp.[mPreviousOwnerZipCode] as mprevzip
,mp.[signDate]
,mp.[sTitle]
,ms.County As boatcnty
,ms.Registration_No As aRegNo
,ms.Motor_MFG_Make AS aMake
,ms.Boat_MFG_Make as mMake
,ms.Motor_MFG_Model as ModelNoA
,ms.Boat_MFG_Model as ModelNoM
,ms.Boat_Year_Built as boatYrBuilt
,ms.Motor_Year_Built as motorYrBuilt
,ms.[Length] as blength
,ms.HullMaterial as hullmaterial
,ms.HorsePower as mhorsepower
,ISNULL(ms.HorsePowerType,'NA') as rdlmhorsepType
,ms.Boat_DatePurchased as datePurchased
,ms.Motor_DatePurchased as mdatePurchased
,ISNULL(ms.Boat_PurchaseType,'NA') as PurchaseType
,ms.Motor_PurchaseType as rblmPurchasedType
,ms.MotorCost as motorCost
,ms.BoatCost as boatCost
,ms.FileDate
,ms.boatIssues as rblIssues
,ms.motorIssues as mrblIssues
,ms.boatIssuesDetails as functionalIsses
,ms.motorIssuesDetails as mfunctionalIsses
,mf.TypeOfVessel as vesseltypeUse
,mf.VesselName as vesselname
,mf.HorspwrTypOfEngine as vesselhorsepEngine
,mf.CoastGuardNo AS coastGuardNumber
,mf.YearPurchased as VesselYrPurchased
,mf.PurchaseType as PurchasedNew
,mf.AmtOfPurchase as VesselPurchaseAmt
,mf.HomePort as homePort
,mf.WhereDocked as whereDocked
,mf.Boat_Motor_Accsry_Equip as bmequipAccessoryList
,mf.vesselLength as vessellength
,mf.YrVesselBuilt as vesselYrBult
,mf.VHullMaterial as VesselHullMateria
FROM MarineInfo m
INNER JOIN BoatMarincePurchaserInfo mp ON m.pid = mp.pid
INNER JOIN MarineScheduleD ms ON m.pid = ms.pid
INNER JOIN MarineFedVesselInfo mf ON m.pid = mf.pid
WHERE m.pid = @pin
END
C#
private void getRecs(int pin)
{
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("sp_AllRecs", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@pin", pin);
cmd.Parameters.Add(p1);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dtPrevRecords = new DataTable();
sda.Fill(dtPrevRecords);
if (dtPrevRecords.Rows.Count > 0)
{
Repeater2.DataSource = dtPrevRecords;
Repeater2.DataBind();
ViewState["CurrentTable"] = dtPrevRecords;
}
}
pageLoad() event
if (!Page.IsPostBack)
{
this.getRecs(pid);
}
|
|
|
|
|
There's nothing obviously wrong with that code. Have you tried running the query in SQL Server Management Studio, to make sure you're getting the right data back?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes, I have run it in SSMS and it displayed the correct data but geez why it is not displaying on the form is beyond me.
|
|
|
|
|
Just for my sanity, it is C# that calls the SP and references CurrentTable datatable correct?
private void getRecs(int pin)
{
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("sp_AllRecs", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@pin", pin);
cmd.Parameters.Add(p1);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dtPrevRecords = new DataTable();
sda.Fill(dtPrevRecords);
if (dtPrevRecords.Rows.Count > 0)
{
Repeater2.DataSource = dtPrevRecords;
Repeater2.DataBind();
ViewState["CurrentTable"] = dtPrevRecords;
}
}
Something should be referencing PopulateRecord method to populate those values and load them to the form and that is not happening.
|
|
|
|
|
That replaces the second table; it doesn't read anything from it.
The PopulateRecord method is called from OnClickNext , so long as certain conditions have been met:
protected void OnClickNext(object sender, EventArgs e)
{
DataTable dt = LoadTable1(false);
if (dt != null)
{
...
myMultiView.ActiveViewIndex += 1;
if (myMultiView.ActiveViewIndex != 3)
{
PopulateRecord(dt);
}
}
}
Those conditions are the same as in the code you posted[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Sorry, I am going to stop bothering you after this question.
You have given me more than enough of your time and assistance.
So, as far as the OnClickNext() method is concerned, the only conditions that have to be met are that those four form fields have values which they do.
Am I missing some other conditions that have to be met from that method?
modified 9-Oct-17 11:11am.
|
|
|
|
|
The two conditions which need to be met are:
- The first table (
CurrTable ) exists - ie: something has called LoadTable1(true) before OnClickNext is called; and myMultiView.ActiveViewIndex is not equal to 2 at the start of the method.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
<pre>using System;
using System.ServiceProcess;
using System.Data.SqlClient;
using System.IO;
using System.Timers;
using System.Configuration;
using System.Data.OleDb;
using System.Data;
namespace MoveToDesiredDest
{
public partial class Service1 : ServiceBase
{
long delay = 5000;
protected string LogPath = ConfigurationManager.AppSettings["LogPath"];
protected string MoveFilePathPath = ConfigurationManager.AppSettings["MovePath"];
public Service1()
{
InitializeComponent();
Timer timer1 = new Timer();
timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer1.Interval = delay;
timer1.Enabled = true;
timer1.Start();
}
protected override void OnStart(string[] args)
{
WriteLog("Service started");
if (!Directory.Exists(LogPath))
{
Directory.CreateDirectory(LogPath);
}
try
{
delay = Int32.Parse(ConfigurationManager.AppSettings["IntervalInSeconds"]) * 1000;
}
catch
{
WriteLog("IntervalInSeconds key/value incorrect.");
}
if (delay < 5000)
{
WriteLog("Sleep time too short: Changed to default(5 secs).");
delay = 5000;
}
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
write();
Upload();
}
private void write()
{
string sourcepath = ConfigurationManager.AppSettings["sourcepath"];
WriteLog("Set source path");
string[] sourcefiles = Directory.GetFiles(sourcepath);
WriteLog("Get soutce path file");
foreach (string childfile in sourcefiles)
{
WriteLog("Start to find file from loop");
string sourceFileName = new FileInfo(childfile).Name;
string destinationPath = ConfigurationManager.AppSettings["destinationPath"];
string destinationFileName = sourceFileName;
string sourceFile = Path.Combine(sourcepath, sourceFileName);
WriteLog("Get file from source to destination");
string destinationFile = Path.Combine(destinationPath, destinationFileName);
WriteLog("Ready to copy");
File.Copy(sourceFile, destinationFile, true);
WriteLog("File copied");
File.Delete(sourceFile);
WriteLog("File deleted");
}
}
protected void Upload()
{
string excelPath = ConfigurationManager.AppSettings["ExcelPath"];
DirectoryInfo d = new DirectoryInfo(excelPath);
FileInfo[] Files = d.GetFiles("*.xls");
string str = "";
WriteLog("ready to enter into loop");
foreach (FileInfo file in Files)
{
str = file.Name;
string sourceFilePath = ConfigurationManager.AppSettings["SourceFilePath"];
string SourceFileName = Path.Combine(sourceFilePath, str);
string destinationFilePath = ConfigurationManager.AppSettings["MovePath"];
string destinationFileName = destinationFilePath + str;
WriteLog("get file :" + str);
String strConnection = ConfigurationManager.AppSettings["constr"];
WriteLog("declare database connection");
string path = excelPath + str;
string excelConnectionString = @"Microsoft.Jet.OLEDB.4.0;Data Source=" + path +";Extended Properties=Excel 8.0;HDR =YES;Persist Security Info=False";
WriteLog("declare excel oledb connection");
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
OleDbCommand cmd = new OleDbCommand("Select [ID],[Name] from [Sheet1$]", excelConnection);
excelConnection.Open();
WriteLog("oledb open");
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
sqlBulk.DestinationTableName = "Test1";
WriteLog("ready to insert into database");
sqlBulk.WriteToServer(dReader);
WriteLog("data inserted");
excelConnection.Close();
WriteLog("Process completed");
File.Move(SourceFileName, destinationFileName);
WriteLog("File moved to bak folder");
excelConnection.Close();
cmd.Dispose();
}
}
protected override void OnStop()
{
WriteLog("service stopped");
}
protected void WriteLog(string Msg)
{
FileStream fs = new FileStream(LogPath + "\\" + System.DateTime.Today.ToString("yyyyMMdd") + ".log", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(System.DateTime.Now.ToString() + ": " + Msg);
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
}
}
}
|
|
|
|
|
Nor should it. That code does not target SQL server; it uses OleDb to target the excel files in a directory.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
Thanks for the quote buddy......... i think you miss something, the connection string i declared that was in Appconfig .... '
strConnection ' and you doesn't even wanted to know where is my connection string for SQL.......?? but you commented on me. good. Please check the code properly.. And another thing please don't give the free Quote without judgement. Thanks
|
|
|
|
|