I am trying to store a result of a candidate but it will return a 0; nothing else, and it will always increment the value of cot if value if arraylist are match or not match but it will increase the value of cot.
Please help me - thanks in advance
public void get_result()
{
int cot = 0, inc = 0;float final_result=0;
ArrayList arr = new ArrayList();
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("(select correct from quiz_question)", con);
con.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
for (int j = 0; dr.Read(); j++)
{
inc++;
string ans = dr[0].ToString();
arr.Add(ans);
Response.Write(arr.ToString());
}
ArrayList arr1 = new ArrayList();
arr1 = (ArrayList)Session["correct_ans"];
for(int l = 0; l < inc; l++)
{
if(arr[l]!=arr1[l])
{
cot++;
}
}
Session["result"] = (Double)((cot/100)*100);
//final_result = ((cot) / 100);
//final_result = final_result * 100;
//Session["result"] = (float)final_result;
//Response.Write(final_result);
}
Related
SqlCommand cmd = new SqlCommand("export_excel", cn);
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
System.Data.DataTable dt = new System.Data.DataTable();
da.Fill(dt);
int k = dt.Columns.Count;
int l = dt.Rows.Count;
if (dt.Rows.Count > 0)
{
Microsoft.Office.Interop.Excel.ApplicationClass XcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
XcelApp.Application.Workbooks.Add(Type.Missing);
for (int i = 0; i < k; i++)
{
XcelApp.Cells[1, i + 1] = dt.Columns[i].ToString();
}
for (int i = 2; i <= l; i++)
{
for (int j = 1; j <= k; j++)
{
XcelApp.Cells[i, j] = dt.Rows[i - 2][j - 1].ToString();
}
}
XcelApp.Visible = true;
}
My data in SQL Server:
And this is the data in the Excel file:
The Excel file returns reversed value with database, I don't know how to handle it, hope the masters answer!
I have 2 tables in my database one of which holds things and the other prices. I designed a form which retrieves thing names as checkboxes and adds textboxes against them. They are all created in runtime when the form loads. They all are named in loops accordingly and when form loads I can see them arranged in the way I wanted to. But I cannot refer them in design time in the form as you can see in below where TextBox1.Text is. May you suggest me how I can do this?. Thanks by now. Below is the code I wrote for controls:
public Form4()
{
InitializeComponent();
SqlConnection conn = new SqlConnection("Data Source=ZEFIRAN\\SQLEXPRESS; Initial Catalog=Lab; Integrated Security=true");
conn.Open();
string say = "SELECT COUNT(DISTINCT Thing) FROM Sale";
SqlCommand cmd = new SqlCommand(say, conn);
Int32 ts = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
for (int i = 0; i < ts; i++)
{
TextBox tb = new TextBox();
tb.Width = 50;
tb.Height = 25;
tb.Name = "TextBox" + (i + 1).ToString();
tb.Text = i + 1 + ". Test";
panel1.Controls.Add(tb);
int x = 150;
int y = 40;
tb.Location = new System.Drawing.Point(x, 40 + i * y);
}
for (int i = 0; i < ts; i++)
{
TextBox tb = new TextBox();
tb.Width = 50;
tb.Height = 25;
tb.Name = "TextBox" + (2 * i + 1).ToString();
tb.Text = i + 1 + ". Test";
panel1.Controls.Add(tb);
int x = 210;
int y = 40;
tb.Location = new System.Drawing.Point(x, 40 + i * y);
}
List<string> seyler = new List<string>();
conn.Open();
string query = "SELECT Thing FROM Things ORDER BY TNo";
using (SqlCommand cmdtest = new SqlCommand(query, conn))
{
using (SqlDataReader dr = cmdtest.ExecuteReader())
{
while (dr.Read())
{
seyler.Add(dr.GetString(0));
}
}
}
string toplam = "SELECT COUNT(Thing) FROM Things";
SqlCommand tplm = new SqlCommand(toplam, conn);
Int32 top1 = Convert.ToInt32(tplm.ExecuteScalar());
for (int j = 0; j < top1; j++)
{
CheckBox chb = new CheckBox();
panel1.Controls.Add(chb);
int t = 50;
int u = 40;
chb.Location = new System.Drawing.Point(t, 40 + j * u);
chb.Width = 100;
chb.Height = 25;
chb.Text = seyler[j];
chb.Name = "cb" + (j + 1).ToString();
}
conn.Close();
TextBox1.Text = "";
}
string[] SName = Request.Form.GetValues("Description");
string[] Email = Request.Form.GetValues("Email");
DataTable dtable = dt();
for (int i = 0; i <= SName.Length - 1; i++)
{
DataRow row1 = dtable.NewRow();
row1["Description"] = SName[i];
row1["Email"] = Email[i];
DAL.DMSS insertdata = new DMSS();
insertdata.INSERT_DATA(loggeduser.SUBSIDIARY_CD, input, SName[i], Email[i]);
}
above are my code to get the data from dynamic row add.
if i have 2 rows,data i get is :
now i want to add 1 more data ,sequence number
tried this code but not working..
for (int i = 0; i <= SName.Length - 1; i++)
{
if (i.length <2 )
{
string strvalue = i.PadLeft(2, '0');
}
else
{
string strvalue = i;
}
DataRow row1 = dtable.NewRow();
row1["Description"] = SName[i];
row1["Email"] = Email[i];
DAL.DMSS insertdata = new DMSS();
insertdata.INSERT_DATA(loggeduser.SUBSIDIARY_CD, input, SName[i], Email[i], strvalue);
}
for (int i = 0; i <= SName.Length - 1; i++)
{
var rowNumber = (i + 1).ToString("0#");
}
I have a textbox for data entry and 10 textboxes for showing data. 10 viewer textboxes are visible=false by default. For example when I enter textbox count to be "3" , only 3 textboxes should be visible. (Then I can do whatever I want with those textboxes)
Here's my code;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
SqlCommand pencere = new SqlCommand("select COUNT (distinct(grup)) as ürün from fiyat", conn);
SqlCommand pencereisimleri = new SqlCommand("select distinct(grup) as ürün from fiyat", conn);
conn.Open();
SqlDataReader dr = pencere.ExecuteReader();
SqlDataAdapter da = new SqlDataAdapter(pencereisimleri);
DataTable dt1 = new DataTable();
da.Fill(dt1);
List<String> stringArr = new List<String>();
for (int a = 0; a < dt1.Rows.Count; a++)
{
TextBox[a].Visible = true;
TextBox[a].Text = "Open Textbox";
}
Not sure, if you're going for this, but how about adding the text boxes to a collection, like an array or List?
Example:
private List<TextBox> boxes = new List<TextBox>();
boxes.Add(textBox1);
boxes.Add(textBox2);
// etc...
Then you could use a foreach (or for) loop, to go through your boxes:
for (int a = 0; a < dt1.Rows.Count; a++)
{
boxes[a].Visible = true;
boxes[a].Text = "Open Textbox";
}
int count = dt1.Rows.Count;
foreach (Control x in this.Controls)
{
if (count == 0)
break;
if (x is TextBox)
{
((TextBox)x).Text = "Open Textbox";
((TextBox)x).Visible = true;
count--;
}
}
you can bind visibility to an array
Visibility[] tbViz = new Visibility[10];
public Visibility[] TbViz { get { return tbViz; } }
public void UpdateViz(int num)
{
for (int i = 0; i < tbViz.Length; i++)
{
if (i < num)
tbViz[i] = System.Windows.Visibility.Visible;
else
tbViz[i] = System.Windows.Visibility.Hidden;
}
}
Visisbility="{binding path=TbViz[0]}"
if winforms same approach
public void UpdateViz(int num)
{
for (int i = 0; i < tbViz.Length; i++)
tbViz[i].Visisble = (i < num);
}
I want to use Excel's buil-in function called LINEST() to do regression analysis in .net.
I am able to use the function with squred matrix array, but when it is not square matrix say of order[12,3] then it gives error as:
LinEst method of WorksheetFunction class failed
Please help me out with this as it is very important for me to complete this code.
This is my complete code:
System.Data.DataTable dt = new System.Data.DataTable();
SqlCommand cmd =new SqlCommand("Select QtytoTransfer from DEmo ",con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
List<double> yDatapoints =new List<double>();
foreach (DataRow dr in dt.Rows)
{
yDatapoints.Add(Convert.ToDouble( dr["QtytoTransfer"].ToString()));
}
System.Data.DataTable dt1 = new System.Data.DataTable();
SqlCommand sqlcmd = new SqlCommand("Select CurrentQoh,QtySold,GameTime from DEmo ", con);
SqlDataAdapter adp1 = new SqlDataAdapter(sqlcmd);
adp1.Fill(dt1);
double[,] xAll = new double[dt1.Rows.Count, dt1.Columns.Count];
for (int i = 0; i < dt1.Rows.Count; ++i)
{
for (int j = 0; j < dt1.Columns.Count; ++j)
{
xAll[i, j] = Convert.ToDouble(dt1.Rows[i][j].ToString());
}
}
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;
object[,] reslut = (object[,])wsf.LinEst(yDatapoints.ToArray(), xAll, missing, true);
if your xAll has a dimension of [12,3] your yDataPoints length should be 3 for proper functioning of LinEst().
using System;
namespace InteropExcel {
class Program {
static void Main(string[] args) {
Random rand = new Random();
double[] yDatapoints = new double[3];
for (int i = 0; i < 3; i++) {
yDatapoints[i]=rand.Next(20, 60);
}
double[,] xAll = new double[12, 3];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 3; j++) {
xAll[i, j] = rand.Next(2, 100);
}
}
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;
object[,] result = (object[,])wsf.LinEst(yDatapoints, xAll, Type.Missing, true);
}
}
}
The column size of xAll should be equal to the length of yDataPoints array. Please try and let me know.
Here's an implementation of Excel's LINEST() function in C#. It might be easier than creating a dependency on the Microsoft.Office.Interop.Excel DLL file.
This returns the slope for a given set of data, normalized using the same "least squares" method that LINEST() uses:
public static double CalculateLinest(double[] y, double[] x)
{
double linest = 0;
if (y.Length == x.Length)
{
double avgY = y.Average();
double avgX = x.Average();
double[] dividend = new double[y.Length];
double[] divisor = new double[y.Length];
for (int i = 0; i < y.Length; i++)
{
dividend[i] = (x[i] - avgX) * (y[i] - avgY);
divisor[i] = Math.Pow((x[i] - avgX), 2);
}
linest = dividend.Sum() / divisor.Sum();
}
return linest;
}
Also, here's a method I wrote to get the "b" (y-intercept) value that Excel's LINEST function generates.
private double CalculateYIntercept(double[] x, double[] y, double linest)
{
return (y.Average() - linest * x.Average());
}
Since these methods only work for one set of data, I would recommend calling them inside of a loop if you wish to produce multiple sets of linear regression data.
This link helped me find my answer: https://agrawalreetesh.blogspot.com/2011/11/how-to-calculate-linest-of-given.html