Checkbox value stored and inserted multiple times - c#

I have a program where i need to register questions. A question can have 1 or multiple right answers, as checked in the method checkTaskType().
All ints are declared in the start of the code.
Code runs fine and works, problem is that if i want to register multiple questions without closing the form, the value from previouse checkbox will be registered as a correct answer on the next question.
-- Dont mind DB design, thats a temporary solution
//Method for insert to DB
private void InsertQAData()
{
using (var con = new MySqlConnection(_connectionString))
{
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =
#"INSERT INTO task(tasktext, alt_1, alt_2, alt_3, alt_4, alt_5, illustration_link, task_type, alt_1_correct, alt_2_correct, alt_3_correct, alt_4_correct, alt_5_correct)
VALUES (#taskText, #alt_1, #alt_2, #alt_3, #alt_4, #alt_5, #illustration_link, #taskType, #corrAlt1, #corrAlt2, #corrAlt3, #corrAlt4, #corrAlt5);";
cmd.Parameters.AddWithValue("#taskText", txtQuestion.Text);
cmd.Parameters.AddWithValue("#alt_1", txtAlt_1.Text);
cmd.Parameters.AddWithValue("#alt_2", txtAlt_2.Text);
cmd.Parameters.AddWithValue("#alt_3", txtAlt_3.Text);
cmd.Parameters.AddWithValue("#alt_4", txtAlt_4.Text);
cmd.Parameters.AddWithValue("#alt_5", txtAlt_5.Text);
cmd.Parameters.AddWithValue("#illustration_link", txtLink.Text);
cmd.Parameters.AddWithValue("#taskType", taskType);
cmd.Parameters.AddWithValue("#corrAlt1", corrAlt1);
cmd.Parameters.AddWithValue("#corrAlt2", corrAlt2);
cmd.Parameters.AddWithValue("#corrAlt3", corrAlt3);
cmd.Parameters.AddWithValue("#corrAlt4", corrAlt4);
cmd.Parameters.AddWithValue("#corrAlt5", corrAlt5);
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
lblInserted.Show();
}
// Method for checking if a question has 1 or multiple answers
private void checkTaskType()
{
if (checkBox1.Checked)
{
count += 1;
corrAlt1 = 1;
}
if (checkBox2.Checked)
{
count += 1;
corrAlt2 = 1;
}
if (checkBox3.Checked)
{
count += 1;
corrAlt3 = 1;
}
if (checkBox4.Checked)
{
count += 1;
corrAlt4 = 1;
}
if (checkBox5.Checked)
{
count += 1;
corrAlt5 = 1;
}
if (count == 1)
{
taskType = "oneAlternative";
}
if (count > 1)
{
taskType = "multipleAlternatives";
}
}

After calling the checkTaskType() call a method ClearResult() like:
private void ClearResult()
{
count = 0;
corrAlt1 = 0;
corrAlt2 = 0;
corrAlt3 = 0;
corrAlt4 = 0;
corrAlt5 = 0;
taskType = string.Empty;
}
This will help you after a question filled and stored to DB, reInitalize the varibles to default state.
So
checkTaskType();
//fill DB with values
//...
ClearResult();

Related

Excel empty fields

I need to update the DB based on the fields in excel, but i need to check if 5 fields in a row in one column are empty, then stop updating the DB.
foreach (var sheets in workbook.Worksheets)
{
SqlCommand comm = new SqlCommand(query, conn);
comm.CommandType = System.Data.CommandType.Text;
var start = sheets.Dimension.Start;
var end = sheets.Dimension.End;
for (int i = start.Row + 1; i < end.Row; i++)
{
object columnE = sheets.Cells[i, 4].Value;
string aa = columnE.ToString();
if (aa.StartsWith("E") && aa.EndsWith("ETO"))
{
comm.Parameters.AddWithValue("#columnE", columnE);
object columnB = sheets.Cells[i, 1].Value;
comm.Parameters.AddWithValue("#columnB", columnB);
}
else if (string.IsNullOrEmpty(aa) || !aa.StartsWith("E") && !aa.EndsWith("ETO"))
{
conn.Close();
}
comm.ExecuteNonQuery();
comm.Parameters.Clear();
}
}
I have a logic when the first fields is null or empty or not what is intended to be to stop updating, but i can't find a way to check that for 5 fields in a row.
I have found the solution, here it is for the future people.
foreach (var sheets in workbook.Worksheets)
{
conn.Open();
result = sheets.ToString();
SqlCommand comm = new SqlCommand(query, conn);
comm.CommandType = System.Data.CommandType.Text;
var start = sheets.Dimension.Start;
var end = sheets.Dimension.End;
int counter = 0;
for (int i = start.Row + 1; i < end.Row; i++)
{
object columnE = sheets.Cells[i, 5].Value;
string aa = "";
if (columnE != null)
{
aa = columnE.ToString();
}
object columnB = sheets.Cells[i, 2].Value;
if (aa.StartsWith("E") && aa.EndsWith("ETO") && conn.State == System.Data.ConnectionState.Open)
{
comm.Parameters.AddWithValue("#columnE", columnE);
comm.Parameters.AddWithValue("#columnB", columnB);
comm.ExecuteNonQuery();
comm.Parameters.Clear();
}
if (columnE == null)
{
counter++;
}
if (counter == 5)
{
conn.Close();
}
}
}
Just count to 5 where you have true logic otherwise reset the counter to start over.
Int counter =0;
for (){
If (logictrue()){
counter++;
if(counter==5)
Update();
} else
counter=0;
}

Convert Rank to percentage C#

I am trying to display percentage based on the rank of each record returned in search. I want it to loop through each item but it only loops through the first item as many times as I have items. For instance if it found 4 results it would display the rank of the first one on all 4 results.
Any suggestions to get it to display each rank separately and convert it to percentage?
private void BindRpt()
{
if (string.IsNullOrEmpty(txtSearch.Text)) return;
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
cmd.CommandText = "select Distinct Rank, SUBSTRING(ColumnA, 1, 500) AS ColumnA, ColumnB, ColumnC, ColumnD, ColumnE from FREETEXTTABLE (TABLE , ColumnA, '" + Search.Text + "' ) S, TABLE C WHERE c.ID = S.[KEY] order by Rank Desc";
DataTable dt = new DataTable();
adapter.SelectCommand = cmd;
adapter.Fill(dt);
PagedDataSource pgitems = new PagedDataSource();
pgitems.DataSource = dt.DefaultView;
pgitems.AllowPaging = true;
pgitems.PageSize = 3;
pgitems.CurrentPageIndex = PageNumber;
if (pgitems.Count > 1)
{
rptPaging.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i <= pgitems.PageCount - 1; i++)
{
pages.Add((i + 1).ToString());
}
rptPaging.DataSource = pages;
rptPaging.DataBind();
lblSentence.Visible = true;
lblSearchWord.Visible = true;
lblSearchWord.Text = txtSearch.Text;
}
else
{
rptPaging.Visible = false;
lblSentence.Visible = true;
lblSentence.Text = "Results were found for";
lblSearchWord.Visible = true;
lblSearchWord.Text = txtSearch.Text;
}
rptResults.DataSource = pgitems;
rptResults.DataBind();
cn.Close();
}
protected void rptResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["HTAA"].ConnectionString);
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
cmd.CommandText = "select Distinct Rank, SUBSTRING(ColumnA, 1, 500) AS ColumnA, ColumnB, ColumnC, ColumnD, ColumnE from FREETEXTTABLE (TABLE , ColumnA, '" + Search.Text + "' ) S, TABLE C WHERE c.ID = S.[KEY] order by Rank Desc";
int number = Page.Items.Count;
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
int firstrank = dr.GetInt32(0);
while (dr.Read())
{
int rank = dr.GetInt32(0);
int percentage = (rank / firstrank) * 100;
Label lblpre = (Label)e.Item.FindControl("lblRank");
lblpre.Text = percentage.ToString();
}
}
dr.Close();
cn.Close();
}
After a chat, I have a better handle on things. A way to do this;
Create a private field on your code behind file.
private int topRanked = 0;
In your Bind method()
private void Bind()
{
...
DataTable dt = new DataTable();
adapter.SelectCommand = cmd;
adapter.Fill(dt);
topRanked = (int)dt.Rows[0]["Rank"];
Now, make your OnItemDataBound method;
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
var dataItem = e.Item.DataItem as DataRowView;
int rank = (int) dataItem["Rank"];
var percentage = ((double)topRanked / rank) * 100;
Label label = (Label)e.Item.FindControl("labelRank");
label.Text = percentage.ToString();
}
as mentioned. I don't believe it's the best answer, but it is an answer. I'm sure a stored procedure, or even a better sql method could probably calculate this and not leave you making calculations in code.
Can you try with while(dr.Read()) instead of "if"?
You will want to loop over the result set
while (dr.Read())
{
int rank = dr.GetInt32(0);
int percentage = (rank / rank) * 100;
Label lblpre = (Label)e.Item.FindControl("lblRank");
lblpre.Text = rank.ToString();
}
"but it only loops through the first item" - because you have a for and checks for i <= 0
for (int i = 0; i <= 0; i++)
{
....
}
You just don't need this for statement but rather use
if (dr != null)
using (dr)
{
while (dr.Read())
{
..
}
}
It's always better to use using when dealing with db connection objects so the resources used by these objects are properly disposed after it's been used.

Search page with empty TextBox returns - Null or empty full-text predicate

I work on a search page. I am using 2 repeaters. First to display the results of the search and second to display paging.
The query works just fine if I exclude postback. I can type something in the search box and it appears on the screen. Without the postback, the problem is when I click to go to the second page, I lose the paging repeater. That means I cannot go back to the first page.
So I need this postback to work.
The problem is when the page first loads the text box is empty, therefore I get the following error: "Null or empty full-text predicate."
How to get around it?
Here my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRpt();
}
}
private void BindRpt()
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["blabla"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
cmd.CommandText = "select Distinct Rank, columnA, columnB, columnC from FREETEXTTABLE (TABLE, columnA , '" + Search.Text + "' ) S, TABLE C WHERE c.columnID = S.[KEY] order by Rank Desc";
DataTable dt = new DataTable();
adapter.SelectCommand = cmd;
adapter.Fill(dt);
PagedDataSource pgitems = new PagedDataSource();
pgitems.DataSource = dt.DefaultView;
pgitems.AllowPaging = true;
pgitems.PageSize = 2;
pgitems.CurrentPageIndex = PageNumber;
if (pgitems.Count > 1)
{
rptPaging.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i <= pgitems.PageCount - 1; i++)
{
pages.Add((i + 1).ToString());
}
rptPaging.DataSource = pages;
rptPaging.DataBind();
}
else
{
rptPaging.Visible = false;
}
rptResults.DataSource = pgitems;
rptResults.DataBind();
}
public int PageNumber
{
get
{
if(ViewState["PageNumber"] != null)
{
return Convert.ToInt32(ViewState["PageNumber"]);
}
else
{
return 0;
}
}
set
{ ViewState["PageNumber"] = value; }
}
protected void rptPaging_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
BindRpt();
}
protected void btnGo_Click(object sender, EventArgs e)
{
BindRpt();
}
Try adding the following as the first line of BindRpt()
if (string.IsNullOrEmpty(Search.Text)) return;
Update following condition:
if (pgitems.Count > 1)
to
if (pgitems.Count > 0)

Reading Entries From Access Database Into DataGridView Using C#

For some reason when I try to read in numbers from an Access database using this code, I just get blank entries in my data grid. I can read strings in fine. Anyone know why this may be? And yes, the actual data type for the unread entries in Access is a NUMBER.
string strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Employees.mdb";
string strSql = "SELECT * FROM tbl_employees";
OleDbConnection con = new OleDbConnection(strProvider);
OleDbCommand cmd = new OleDbCommand(strSql, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataReader dr = cmd.ExecuteReader();
int columnCount = dr.FieldCount;
for (int i = 0; i < columnCount; i++)
{
dgv.Columns.Add(dr.GetName(i).ToString(), dr.GetName(i).ToString());
}
string[] rowData = new string[columnCount];
while (dr.Read())
{
for (int k = 0; k < columnCount; k++)
{
if (dr.GetFieldType(k).ToString() =="System.Int32")
{
rowData[k] = dr.GetInt32(k).ToString();
}
if (dr.GetFieldType(k).ToString() == "System.String")
{
rowData[k] = dr.GetString(k);
}
}
dgv.Rows.Add(rowData);
}
I suggest you try stepping through your code in the debugger so you can see what's happening. My first guess would be that your numeric fields aren't returned as Int32, perhaps they are floats or decimals instead.
If for some reason you can't step through it, try something like this:
if (dr.GetFieldType(k).ToString() =="System.Int32")
{
rowData[k] = dr.GetInt32(k).ToString();
}
else if (dr.GetFieldType(k).ToString() == "System.String")
{
rowData[k] = dr.GetString(k);
}
else
{
rowData[k] = dr.GetFieldType(k).ToString();
}
That will let you see what type of value is in the fields that didn't get displayed.

Taking values into an array of textboxes (ASP.NET using C#)

So I have an array of textboxes dynamically appear (The number of textboxes depends upon how a number from a database). They draw to the screen just fine.
i = 0;
while (i < size)
{
pnlTxtBoxes.Controls.Add(labels[i]);
pnlTxtBoxes.Controls.Add(txtBoxes[i]);
pnlTxtBoxes.Wrap = true;
i++;
}
Like I said, the textboxes appear and the labels are displaying correctly. But when I go to retrieve the text from them, I get the error "Object reference not set to an instance of an object."
i = 0;
while (i < size)
{
values[i] = txtBoxes[i].Text;
txtBoxes[i].Visible = false;
labels[i].Visible = false;
i++;
}
Does anybody have an idea as to why I'm getting this error (and what I can do to fix it)?
EDIT: Here is all of the code. This is just a development DB, so I am not worried about showing the password
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
public partial class dieClearanceCalc : System.Web.UI.Page
{
static string connectionString = "database=localhost;database=matedevdb;uid=dev;pwd=123;";
MySqlConnection con = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand("SELECT shapeName FROM tblShapes;");
MySqlDataReader reader;
int size;
TextBox[] txtBoxes;
Label[] labels;
protected void Page_Load(object sender, EventArgs e)
{
cmd.Connection = con;
try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
shapeSelection.Items.Add(reader.GetString(0));
}
reader.Close();
}
catch (Exception ex)
{
Response.Write("<p style='Color:red'>Error:<br/>" + ex + "</p>");
}
}
protected void shapeSelected(object sender, EventArgs e)
{
string[] labelTxt;
int i = 0;
//Make current elements invisable
lblShape.Visible = false;
shapeSelection.Visible = false;
btnSelectShape.Visible = false;
// find the size of the arrays
cmd.CommandText = "SELECT COUNT(varID) FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
reader = cmd.ExecuteReader();
if (reader.Read())
{
size = reader.GetInt32(0);
}
reader.Close();
labelTxt = new string[size];
labels = new Label[size];
txtBoxes = new TextBox[size];
// gather the labels from the db
cmd.CommandText = "SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
reader = cmd.ExecuteReader();
i = 0;
while (reader.Read())
{
labelTxt[i] = reader.GetString("varDesc");
i++;
}
reader.Close();
i = 0;
while (i < size)
{
labels[i] = new Label();
txtBoxes[i] = new TextBox();
labels[i].Text = labelTxt[i];
i++;
}
i = 0;
while (i < size)
{
pnlTxtBoxes.Controls.Add(labels[i]);
pnlTxtBoxes.Controls.Add(txtBoxes[i]);
pnlTxtBoxes.Wrap = true;
i++;
}
btnSendData.Visible = true;
//Response.Write(size); test to see if the size variable is working
Response.Write(size);
}
protected void calc(object sender, EventArgs e)
{
//declarations
formula diagonal, periphery;
string dFormula = "", pFormula = "";
string[] variables;
string[] values;
int i = 0;
//end of declarations
// This value must be retrievd again, because somewhere size is getting a value of 0
cmd.CommandText = "SELECT COUNT(varID) FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
reader = cmd.ExecuteReader();
if (reader.Read())
{
size = reader.GetInt32(0);
}
reader.Close();
variables = new string[size];
values = new string[size];
i = 0;
while (i < size)
{
values[i] = txtBoxes[i].Text;
txtBoxes[i].Visible = false;
labels[i].Visible = false;
i++;
}
btnSendData.Visible = false;
// retrieve the diagonal formula from the db
cmd.CommandText = "SELECT diagonalFormula, peripheryFormula FROM tblShapes WHERE shapeName='" + shapeSelection.SelectedValue + "'";
reader = cmd.ExecuteReader();
while (reader.Read())
{
dFormula = reader.GetString("diagonalFormula");
pFormula = reader.GetString("peripheryFormula");
}
reader.Close();
Response.Write(size);
// gather the variable names from the db
cmd.CommandText = "SELECT varName FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
reader = cmd.ExecuteReader();
while (reader.Read())
{
variables[i] = reader.GetString("varName");
i++;
}
reader.Close();
con.Close();
diagonal = new formula(dFormula, variables, values);
periphery = new formula(pFormula, variables, values);
txtDiagonal.Visible = true;
txtPeriphery.Visible = true;
txtDiagonal.Text = diagonal.getEquation();
txtPeriphery.Text = periphery.getEquation();
}
public static double Evaluate(string expression)
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("expression", string.Empty.GetType(), expression);
System.Data.DataRow row = table.NewRow();
table.Rows.Add(row);
return double.Parse((string)row["expression"]);
}
}
I reckon you initialize the labels and txtBoxes in if IsPostBack block and don't save it in ViewState or Session.
Edit:
saw your code labels and txtBoxes was initialized in shapeSelected method. same problem will happen: they are lost between postback.
So they are empty in event handler when postback because whole Page object was re-created when postback. Asp.net runtime helps to load content from ViewState for control.But for class member variable you have to maintain by yourself.like:
public string NavigateUrl
{
get
{
string text = (string) ViewState["NavigateUrl"];
if (text != null)
return text;
else
return string.Empty;
}
set
{
ViewState["NavigateUrl"] = value;
}
}
Above code comes from:
Understanding ASP.NET View State
http://msdn.microsoft.com/en-us/library/ms972976.aspx
The article also introduces View State and Dynamically Added Controls
An important thing to remember when programming with ASP.NET is that your whole object model on a page gets created with every web request again and again. This means that if you add some controls to you page dynamically in an event that does not happen each page load they are not going to survive the postback if you don't add them again somehow.
You can read about Asp.Net page Life Cycle here: http://msdn.microsoft.com/en-us/library/ms178472.aspx
I don't think there is a standard way to solve your problem, but what you are trying to do can be achieved in several ways. One was already mentioned to you - usage of ViewState. Another one would be hitting database on each post back, and making sure you are recreating the controls each time the page is served. One more way is to somehow encode the data that is required for recreating your controls and make sure that they are passed with each post back. The latter is essentially what ViewState does, but you can do it more efficiently if you want to reduce the size of you ViewState, and thus size in bytes of your postback and page.
Without seeing more code, I can only guess that the problem lies with where you populate the txtBoxes[] array. Make sure there are no null values in that array.

Categories

Resources