am trying to fetch values from HTML and store it in a database. now, am able to fetch values and store it in an arry. I am having difficulty in storing it into database. Here is my code, pls let me know where am doing it wrong.
for (int i = 0; i < strs.Length; i++)
{
if (strs[i] != null)
{
TextBox2.Text += "strs" + "[" + i + "]" + "=" + strs[i] + '\n';
}
}
Label6.Text = strs[18]; //Employee ID
Label7.Text = strs[19] + " " + strs[20]; //Employee Name
int count = 0;
Label8.Text = "";
for (int x = 0; x < strs.Length; x++)
{
if (strs[x] == "\r\n")
count++;
if (strs[x] == "\n")
count++;
if (count == 11)
{
Label8.Text += strs[x] + " "; //Designation is stored here
}
if (count == 12)
break;
}
int a = 0, b = 0, l = 0, k = 0, h = 0, n = 0, j = 0, m = 0,u=0; string[] date1 = new string[50]; string[] inampm = new string[400]; string[] outampm = new string[400]; string[] hrpd = new string[100]; string[][] inarray = new string[400][]; string[][] outarray = new string[400][];
for (int o = 0; o < 400; o++) { inarray[o] = new string[400]; }
for (int o = 0; o < 400; o++) { outarray[o] = new string[400]; } labelno1: if (strs[a] == "Date") { date1[b++] = strs[a+2]; if (inap == outap) { hrpd[u++] = "Can be calculated"; } else { hrpd[u++] = "Cannot be calculated"; } j++; k = 0; l++; a++; m = 0; h = 0; n = 0; inap = 0; outap = 0; }
A.Venkatesan
Microsoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
Pradeep pooj...
Member
2 Points
6 Posts
how to insert Array values into a table in the database.
Feb 15, 2012 07:50 AM|LINK
Hello All,
am trying to fetch values from HTML and store it in a database. now, am able to fetch values and store it in an arry. I am having difficulty in storing it into database. Here is my code, pls let me know where am doing it wrong.
ublic partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int inap = 0, outap = 0;
if (FileUpload1.HasFile)
{
string htmldata;
StreamReader file = new StreamReader(FileUpload1.FileContent);
htmldata = file.ReadToEnd();
TextBox1.Text = HtmlRemoval.StripTagsRegex(htmldata);
string data = TextBox1.Text;
string[] strs = data.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
TextBox2.Text = "";
for (int i = 0; i < strs.Length; i++)
{
if (strs[i] != null)
{
TextBox2.Text += "strs" + "[" + i + "]" + "=" + strs[i] + '\n';
}
}
Label6.Text = strs[18]; //Employee ID
Label7.Text = strs[19] + " " + strs[20]; //Employee Name
int count = 0;
Label8.Text = "";
for (int x = 0; x < strs.Length; x++)
{
if (strs[x] == "\r\n")
count++;
if (strs[x] == "\n")
count++;
if (count == 11)
{
Label8.Text += strs[x] + " "; //Designation is stored here
}
if (count == 12)
break;
}
int a = 0, b = 0, l = 0, k = 0, h = 0, n = 0, j = 0, m = 0,u=0;
string[] date1 = new string[50];
string[] inampm = new string[400];
string[] outampm = new string[400];
string[] hrpd = new string[100];
string[][] inarray = new string[400][];
string[][] outarray = new string[400][];
for (int o = 0; o < 400; o++)
{
inarray[o] = new string[400];
}
for (int o = 0; o < 400; o++)
{
outarray[o] = new string[400];
}
labelno1: if (strs[a] == "Date")
{
date1[b++] = strs[a+2];
if (inap == outap)
{
hrpd[u++] = "Can be calculated";
}
else
{
hrpd[u++] = "Cannot be calculated";
}
j++;
k = 0;
l++;
a++;
m = 0;
h = 0;
n = 0;
inap = 0;
outap = 0;
}
if (strs[a] == "IN")
{
inarray[j][k] = strs[a - 3];
inampm[h++] = strs[(a - 2)];
k++;
inap++;
}
if (strs[a] == "OUT")
{
outarray[l][m] = strs[a - 3];
outampm[n++] = strs[a - 2];
m++;
outap++;
}
SqlConnection con = new SqlConnection("Data Source=xxxxxxxxx;Initial Catalog=xxxx;User ID=Administrator; password=xxxx);
con.Open();
SqlCommand sc = new SqlCommand("insert into Test values('" + date1[0] + "','" + inarray[1][k] + "','" + outarray[1][m] + "','" + "" + "')", con);
sc.ExecuteNonQuery();
con.Close();
if (a == (strs.Length-1))
{
goto labelno2;
}
if (strs[a] == "Date")
{
goto labelno1;
}
else
{
a++;
goto labelno1;
}
labelno2: if (inap == outap)
{
hrpd[u++] = "Can be calculated";
}
else
{
hrpd[u++] = "Cannot be calculated";
}
TextBox3.Text = "";
for (a = 0; a < date1.Length; a++)
{
TextBox3.Text += date1[a] + " " + hrpd[a+1] + " " +"\n";
}
}
else
{
Label2.Text = "No file selected";
}
}
public static class HtmlRemoval
{
/// <summary>
/// Remove HTML from string with Regex.
/// </summary>
public static string StripTagsRegex(string source)
{
string source1 = Regex.Replace(source, "<.*?>", " ");
string source2 = Regex.Replace(source1, "LOG", "");
string source3 = Regex.Replace(source2, "TIME", "");
string source4 = Regex.Replace(source3, "TYPE", "");
return Regex.Replace(source4, "Shift", "");
}
}
}
venkatmca008
Participant
1810 Points
341 Posts
Re: how to insert Array values into a table in the database.
Feb 15, 2012 08:30 AM|LINK
Hi,Please Refer this @Pradeep poojary
http://weblogs.asp.net/jgalloway/archive/2007/02/16/passing-lists-to-sql-server-2005-with-xml-parameters.aspx
Microsoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: how to insert Array values into a table in the database.
Feb 22, 2012 07:22 AM|LINK
Hi
I suggest you use List<List<string>> instead string[][].
Because you can't calculate the length of the html is that right?
So you can use
List<string> list = new List<string>(); List<List<string>> StringArray = new List<List<string>>(); list.Add("string here"); StringArray.Add(list);And then convert it to a string, then store the string to database.
That all.
Hope it helpful.
Thanks.
Dino
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework