i'm working on restaurants programming with c#
working on the daily quantity of the material
i do count of the total that they make on the day then count how many they used and how many remaining on that day
my code is
public void ExecuteQuery(string query)
{
foreach (Control txt in this.Controls)
{
if (txt.GetType() == typeof(TextBox))
if (string.IsNullOrEmpty(txt.Text))
txt.Text = "0";
}
SqlCommand com = new SqlCommand(query, connection);
com.Parameters.Add("#shish_add", SqlDbType.Int).Value = shish_add.Text;
com.Parameters.Add("#bani_add", SqlDbType.Int).Value = bani_add.Text;
com.Parameters.Add("#zingr_add", SqlDbType.Int).Value = zingr_add.Text;
com.Parameters.Add("#tabona_add", SqlDbType.Int).Value = tabona_add.Text;
com.Parameters.Add("#fahita_chkn_add", SqlDbType.Int).Value = fahita_chkn_add.Text;
com.Parameters.Add("#borger_add", SqlDbType.Int).Value = borger_add.Text;
com.Parameters.Add("#kabab_add", SqlDbType.Int).Value = kabab_add.Text;
com.Parameters.Add("#shgef_add", SqlDbType.Int).Value = shgef_add.Text;
com.Parameters.Add("#faheta_met_add", SqlDbType.Int).Value = faheta_met_add.Text;
com.Parameters.Add("#kabda_add", SqlDbType.Int).Value = kabda_add.Text;
com.Parameters.Add("#shawo_met_add", SqlDbType.Int).Value = shawo_met_add.Text;
com.Parameters.Add("#shawo_chkn_add", SqlDbType.Int).Value = shawo_chkn_add.Text;
com.Parameters.Add("#chkn_mash_add", SqlDbType.Int).Value = chkn_mash_add.Text;
com.Parameters.Add("#chkn_red_add", SqlDbType.Int).Value = chkn_red_add.Text;
com.Parameters.Add("#patata_add", SqlDbType.Int).Value = patata_add.Text;
com.Parameters.Add("#shish_take", SqlDbType.Int).Value = shish_take.Text;
com.Parameters.Add("#bani_take", SqlDbType.Int).Value = bani_take.Text;
com.Parameters.Add("#zingr_take", SqlDbType.Int).Value = zingr_take.Text;
com.Parameters.Add("#tabona_take", SqlDbType.Int).Value = tabona_take.Text;
com.Parameters.Add("#fahita_chkn_take", SqlDbType.Int).Value = fahita_chkn_take.Text;
com.Parameters.Add("#borger_take", SqlDbType.Int).Value = borger_take.Text;
com.Parameters.Add("#kabab_take", SqlDbType.Int).Value = kabab_take.Text;
com.Parameters.Add("#shgef_take", SqlDbType.Int).Value = shgef_take.Text;
com.Parameters.Add("#faheta_met_take", SqlDbType.Int).Value = faheta_met_take.Text;
com.Parameters.Add("#kabda_take", SqlDbType.Int).Value = kabda_take.Text;
com.Parameters.Add("#shawo_met_take", SqlDbType.Int).Value = shawo_met_take.Text;
com.Parameters.Add("#shawo_chkn_take", SqlDbType.Int).Value = shawo_chkn_take.Text;
com.Parameters.Add("#chkn_mash_take", SqlDbType.Int).Value = chkn_mash_take.Text;
com.Parameters.Add("#chkn_red_take", SqlDbType.Int).Value = chkn_red_take.Text;
com.Parameters.Add("#patata_take", SqlDbType.Int).Value = patata_take.Text;
com.Parameters.Add("#shish_rom", SqlDbType.Int).Value = shish_rom.Text;
com.Parameters.Add("#bani_rom", SqlDbType.Int).Value = bani_rom.Text;
com.Parameters.Add("#zingr_rom", SqlDbType.Int).Value = zingr_rom.Text;
com.Parameters.Add("#tabona_rom", SqlDbType.Int).Value = tabona_rom.Text;
com.Parameters.Add("#fahita_chkn_rom", SqlDbType.Int).Value = fahita_chkn_rom.Text;
com.Parameters.Add("#borger_rom", SqlDbType.Int).Value = borger_rom.Text;
com.Parameters.Add("#kabab_rom", SqlDbType.Int).Value = kabab_rom.Text;
com.Parameters.Add("#shgef_rom", SqlDbType.Int).Value = shgef_rom.Text;
com.Parameters.Add("#faheta_met_rom", SqlDbType.Int).Value = faheta_met_rom.Text;
com.Parameters.Add("#kabda_rom", SqlDbType.Int).Value = kabda_rom.Text;
com.Parameters.Add("#shawo_met_rom", SqlDbType.Int).Value = shawo_met_rom.Text;
com.Parameters.Add("#shawo_chkn_rom", SqlDbType.Int).Value = shawo_chkn_rom.Text;
com.Parameters.Add("#chkn_mash_rom", SqlDbType.Int).Value = chkn_mash_rom.Text;
com.Parameters.Add("#chkn_red_rom", SqlDbType.Int).Value = chkn_red_rom.Text;
com.Parameters.Add("#patata_rom", SqlDbType.Int).Value = patata_rom.Text;
com.Parameters.Add("#dates", SqlDbType.DateTime).Value = dates.Value.Date;
if (connection.State == ConnectionState.Closed)
connection.Open();
com.ExecuteNonQuery();
connection.Close();
foreach (Control txt in this.Controls)
{
if (txt.GetType() == typeof(TextBox))
if (txt.Text == "0")
txt.Text = "";
}
MessageBox.Show("تمت عملية الاضافة بنجاح", "ملاحظة", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
public void doqury(string strn)
{
string sqlQuery = strn;
ExecuteQuery(sqlQuery);
}
private void button1_Click(object sender, EventArgs e)
{
// check for first time entry
SqlCommand cmd = new SqlCommand("select * from megana_cooking ", connection);
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
//mean there values on database
//now need to check is this a new day or not
SqlCommand cmd1 = new SqlCommand("select * from megana_cooking where dates = #dates", connection);
connection.Open();
SqlDataReader reader1 = cmd.ExecuteReader();
if (reader1.HasRows)
{
//no this is the take valus statment
reader.Close();
doqury("UPDATE megana_cooking SET shish_rom =shish_add - #shish_take ,bani_rom =bani_add - #bani_take ,zingr_rom =zingr_add - #zingr_take ,tabona_rom =tabona_add - #tabona_take ,fahita_chkn_rom =fahita_chkn_add - #fahita_chkn_take ,borger_rom =borger_add - #borger_take ,kabab_rom =kabab_add - #kabab_take ,shgef_rom =shgef_add - #shgef_take ,faheta_met_rom =faheta_met_add - #faheta_met_take ,kabda_rom =kabda_add - #kabda_take ,shawo_met_rom =shawo_met_add - #shawo_met_take ,shawo_chkn_rom =shawo_chkn_add - #shawo_chkn_take ,chkn_mash_rom =chkn_mash_add - #chkn_mash_take ,chkn_red_rom =chkn_red_add - #chkn_red_take ,patata_rom =patata_add - #patata_take ,patata_take =#patata_take,chkn_red_take =#chkn_red_take,chkn_mash_take =#chkn_mash_take,shawo_chkn_take =#shawo_chkn_take,shawo_met_take =#shawo_met_take,kabda_take =#kabda_take,faheta_met_take =#faheta_met_take,shgef_take =#shgef_take,kabab_take =#kabab_take,borger_take =#borger_take,fahita_chkn_take =#fahita_chkn_take,tabona_take =#tabona_take,zingr_take =#zingr_take,bani_take =#bani_take,shish_take =#shish_take ,dates =#dates where dates = #dates ");
}
else
{
// yes new day new values and add value to the remain values from last entry
DialogResult result = MessageBox.Show("سيتم ادخال بيانات ليوم جديد " + Environment.NewLine + " سيقوم بأدخال البيانات دون احتساب عملية السحب", "تأكيد", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
doqury("INSERT INTO megana_cooking ( shish_add ,bani_add ,zingr_add ,tabona_add ,fahita_chkn_add ,borger_add ,kabab_add ,shgef_add ,faheta_met_add ,kabda_add ,shawo_met_add ,shawo_chkn_add ,chkn_mash_add ,chkn_red_add ,patata_add ,dates ) VALUES ( #shish_add ,#bani_add ,#zingr_add ,#tabona_add ,#fahita_chkn_add ,#borger_add ,#kabab_add ,#shgef_add ,#faheta_met_add ,#kabda_add ,#shawo_met_add ,#shawo_chkn_add ,#chkn_mash_add ,#chkn_red_add ,#patata_add ,#dates )");
//insert new quentity done now need to update the remain value
//new quentity + last remain
here need to find the last date that it have value then update and add the new quantity to it
}
}
else
{
//this is first entry
reader.Close();
DialogResult result = MessageBox.Show("سيتم ادخال البيانات الى المنظومة لاول مره " + Environment.NewLine + " سيقوم بأدخال البيانات دون احتساب عملية السحب", "تأكيد", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
doqury("INSERT INTO megana_cooking ( shish_add ,bani_add ,zingr_add ,tabona_add ,fahita_chkn_add ,borger_add ,kabab_add ,shgef_add ,faheta_met_add ,kabda_add ,shawo_met_add ,shawo_chkn_add ,chkn_mash_add ,chkn_red_add ,patata_add ,shish_rom ,bani_rom ,zingr_rom ,tabona_rom ,fahita_chkn_rom ,borger_rom ,kabab_rom ,shgef_rom ,faheta_met_rom ,kabda_rom ,shawo_met_rom ,shawo_chkn_rom ,chkn_mash_rom ,chkn_red_rom ,patata_rom ,dates ) VALUES ( #shish_add ,#bani_add ,#zingr_add ,#tabona_add ,#fahita_chkn_add ,#borger_add ,#kabab_add ,#shgef_add ,#faheta_met_add ,#kabda_add ,#shawo_met_add ,#shawo_chkn_add ,#chkn_mash_add ,#chkn_red_add ,#patata_add ,#shish_add ,#bani_add ,#zingr_add ,#tabona_add ,#fahita_chkn_add ,#borger_add ,#kabab_add ,#shgef_add ,#faheta_met_add ,#kabda_add ,#shawo_met_add ,#shawo_chkn_add ,#chkn_mash_add ,#chkn_red_add ,#patata_add ,#dates )");
}
connection.Close();
}
the question is
i need to find the last date that it have value then update and add the new quantity to it i did mention it in my code
i know i can because when i tried this code on new query on sql management studio it work
SELECT
*
FROM
megana_cooking
ORDER BY
dates
update megana_cooking set shish_rom = shish_rom + '5'
can i make the sql command shorter than this by using sql programmability or server side because i have more than 15 type of the material food
i have weak knowledge about sql
thank you in advance
I have this situation: in DataEntryForm I have a dropdownlist, where user selects a letter number, and according to that inserts other related data.
I plan to change letter's status in other table by choosing in dropdownlist automatically.
I am using this code:
SqlParameter answertoparam = new SqlParameter("answerto", ansTo);
string commandText = "update IncomeLetters set IncomeLetters.docState_ID ='2' where income_number=('" + ansTo + "' )";
SqlCommand findincomelett = new SqlCommand(commandText, conn);
comm.Parameters.Add(answertoparam);
conn.Open();
findincomelett.ExecuteNonQuery();
comm.ExecuteNonQuery();
Unfortunately, the result is nothing.
Server is not giving error, and it simply refreshes the page that is it.
In your posted code, you are passing the SqlParameter as well as passing the value as raw data. Do either of one and preferably pass it as SqlParameter like
SqlParameter answertoparam = new SqlParameter("answertoparam", ansTo);
string commandText = "update IncomeLetters set IncomeLetters.docState_ID = '2' where income_number = #answertoparam";
SqlCommand findincomelett = new SqlCommand(commandText, conn);
findincomelett.Parameters.Add(answertoparam);
conn.Open();
findincomelett.ExecuteNonQuery();
Moreover, you have two SqlCommand object in place and calling two ExecuteNonQuery() on them. correct that ... see below
SqlCommand findincomelett = new SqlCommand(commandText, conn); --1
comm.Parameters.Add(answertoparam); --2
conn.Open();
findincomelett.ExecuteNonQuery(); --1
comm.ExecuteNonQuery(); --2
As far as I understand, the issue is that the correct IncomeLetters.docState_ID is not updated to '2'.
You may want to debug and see what value you are getting in :
string ansTo = ddlAnswerTo.SelectedItem.Value;
The record in the database that you are expecting to be updated may not have the record that satisfies the where clause 'income_number = #answertoparam'
I would like to bring you here full code of the page.
Idea is: I have page for enrollment. I am passing data to DB through stored procedure (DataInserter).
Problem is here: during enrollment, user selects from dropdownlist number of the letter he would like to answer to, and in the end, the status of the letter on other table of DB (IncomeLetters.tbl), would change from "pending"('1') to "issued" ('2').
I guess, I could clear my point to you and thank you for your support!
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MaktubhoConnectionString2"].ConnectionString);
using (SqlCommand comm = new SqlCommand("DataInserter", conn))
{
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
SqlParameter employeeparam = new SqlParameter("EmployeeSentIndex", int.Parse(ddlemployee.SelectedItem.Value));
SqlParameter doctypeparam = new SqlParameter("doctype_ID", int.Parse(ddldoctype.SelectedItem.Value));
SqlParameter doccharparam = new SqlParameter("docchar_ID", int.Parse(ddldocchar.SelectedItem.Value));
SqlParameter authorityparam = new SqlParameter("authority", txtauthority.Text);
SqlParameter subjectparam = new SqlParameter("subject", txtsubject.Text);
DateTime dt = DateTime.Now;
string todasdate = dt.ToString("d", CultureInfo.CreateSpecificCulture("de-DE"));
SqlParameter entrydateparam = new SqlParameter("entrydate", todasdate);
string Pathname = "UploadImages/" + Path.GetFileName(FileUpload1.PostedFile.FileName);
SqlParameter imagepathparam = new SqlParameter("image_path", Pathname);
SqlParameter loginparam = new SqlParameter("login", "jsomon");
comm.Parameters.Add(employeeparam);
comm.Parameters.Add(doctypeparam);
comm.Parameters.Add(doccharparam);
comm.Parameters.Add(authorityparam);
comm.Parameters.Add(subjectparam);
comm.Parameters.Add(entrydateparam);
comm.Parameters.Add(imagepathparam);
comm.Parameters.Add(loginparam);
comm.Parameters.Add("#forlabel", SqlDbType.VarChar, 100);
comm.Parameters["#forlabel"].Direction = ParameterDirection.Output;
FileUpload1.SaveAs(Server.MapPath("~/UploadImages/" + FileUpload1.FileName));
string ansTo = ddlAnswerTo.SelectedItem.Value;
SqlParameter answertoparam = new SqlParameter("answertoparam", ansTo);
string commandText = "update IncomeLetters set IncomeLetters.docState_ID = '2' where income_number = #answertoparam";
SqlCommand findincomelett = new SqlCommand(commandText, conn);
findincomelett.Parameters.Add(answertoparam);
conn.Open();
findincomelett.ExecuteNonQuery();
comm.ExecuteNonQuery();
lblresult.Visible = true;
Image1.Visible = true;
lblresult.Text = "Document number:";
lblnumber.Visible = true;
lblnumber.Text = (string)comm.Parameters["#forlabel"].Value; ;
conn.Close();
}
txtauthority.Text = "";
txtsubject.Text = "";
}
I m sorry if this question asked before, i am new to c# i am working with ms access database i am storing date to ms access date field...
please check what is the error
My error string is following:
failed to convert parameter value from a string to a timespan
The code is:
private void button1_Click(object sender, EventArgs e)
{
//create connection
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=posv.accdb";
conn.Open();
string Expanse_Name = expanse_name.Text;
string Expanse_Cost = expanse_cost.Text;
string Expanse_Date = expanse_date.Value.ToString("m/d/Y");
OleDbCommand cmd = new OleDbCommand("INSERT INTO expanses (Expanse_Name, Expanse_Cost,Expanse_Date) VALUES (#Expanse_Name, #Expanse_Cost,#Expanse_Date)", conn);
if(conn.State == ConnectionState.Open){
cmd.Parameters.Add("#Expanse_Name", OleDbType.VarChar, 20).Value = Expanse_Name;
cmd.Parameters.Add("#Expanse_Cost", OleDbType.UnsignedInt, 20).Value = Expanse_Cost;
cmd.Parameters.Add("#Expanse_Date", OleDbType.VarChar, 20).Value = Expanse_Date.;
try {
cmd.ExecuteNonQuery();
MessageBox.Show("Expanse Added Success fully!");
}catch(OleDbException exps){
MessageBox.Show(exps.Message);
conn.Close();
} // end try
} //end conn state
} // end save function
You need to specify the right datatype for the db variable. It depends what datatype you've chosen in db. So best approach is look at the respective datatype in the database and change it accordingly.
try this:
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=posv.accdb";
conn.Open();
string Expanse_Name = expanse_name.Text;
string Expanse_Cost = expanse_cost.Text;
string Expanse_Date = expanse_date.Value.ToString("m/d/Y");
OleDbCommand cmd = new OleDbCommand("INSERT INTO expanses (Expanse_Name, Expanse_Cost,Expanse_Date) VALUES (#Expanse_Name, #Expanse_Cost,#Expanse_Date)", conn);
if(conn.State == ConnectionState.Open){
cmd.Parameters.Add("#Expanse_Name", OleDbType.VarChar, 20).Value = Expanse_Name;
cmd.Parameters.Add("#Expanse_Cost", OleDbType.UnsignedInt, 20).Value = Expanse_Cost;
cmd.Parameters.Add("#Expanse_Date", OleDbType.DBTimeStamp, 20).Value = Expanse_Date.;
try {
cmd.ExecuteNonQuery();
MessageBox.Show("Expanse Added Success fully!");
}catch(OleDbException exps){
MessageBox.Show(exps.Message);
conn.Close();
} // end try
} //end conn state
}
Change the ToString format on the line line:
string Expanse_Date = expanse_date.Value.ToString("HH:mm:ss.fff");
I think the Expanse_Date should be keep in DateTime and the parameter type for it should be OleDbType.DBTimeStamp. (And please use using blocks.)
private void button1_Click(object sender, EventArgs e)
{
using (var conn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=posv.accdb"))
{
conn.Open();
var Expanse_Name = expanse_name.Text;
var Expanse_Cost = expanse_cost.Text;
var Expanse_Date = expanse_date.Value;
using (var cmd = new OleDbCommand("INSERT INTO expanses (Expanse_Name, Expanse_Cost,Expanse_Date) VALUES (#Expanse_Name, #Expanse_Cost,#Expanse_Date)", conn))
{
cmd.Parameters.Add("#Expanse_Name", OleDbType.VarChar, 20).Value = Expanse_Name;
cmd.Parameters.Add("#Expanse_Cost", OleDbType.UnsignedInt, 20).Value = Expanse_Cost;
cmd.Parameters.Add("#Expanse_Date", OleDbType.DBTimeStamp, 20).Value = Expanse_Date;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Expanse Added Success fully!");
}
catch (OleDbException exps)
{
MessageBox.Show(exps.Message);
conn.Close();
}
}
}
}
I create a web form that contains: Dropdownlist, texbox and rename button.
The general idea is that Dropdownlist contains list of column names of one table in my database.
Then the user select one of these names and enter the new name in the textbox. After that, he click the rename button. The result is rename the selected column in my database.
My code is work well. And it is give exact result.
see my code:
protected void Button1_Click(object sender, EventArgs e)
{
string conString = #"Data Source=FATTO-TOSH\SQLEXPRESS;Initial Catalog=Positions;Integrated Security=True";
if (DropDownList1.SelectedIndex == 0)
using (var con = new SqlConnection(conString))
{
var cmd = new SqlCommand("sys.sp_rename", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#objname", "PositionsReq.skill1")
.SqlDbType = SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("#newname", name.Text)
.SqlDbType = SqlDbType.NVarChar;
cmd.ExecuteNonQuery();
}
if (DropDownList1.SelectedIndex == 1)
using (var con = new SqlConnection(conString))
{
var cmd = new SqlCommand("sys.sp_rename", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#objname", "PositionsReq.skill2")
.SqlDbType = SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("#newname", name.Text)
.SqlDbType = SqlDbType.NVarChar;
cmd.ExecuteNonQuery();
}
if (DropDownList1.SelectedIndex == 2)
using (var con = new SqlConnection(conString))
{
var cmd = new SqlCommand("sys.sp_rename", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#objname", "PositionsReq.skill3")
.SqlDbType = SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("#newname", name.Text)
.SqlDbType = SqlDbType.NVarChar;
cmd.ExecuteNonQuery();
}
}
My question is:
The modification achieved only one time (regard to one column) because when I change the name from skill1 to Sk for example. May in other time, the user want to modify Sk to other name. the code doesn't work because it is initialize as column name is skill1 only. do you have an idea how to generalize the code to work whatever the name of the column?
thank you
Instead of using DropDownList1.SelectedIndex why don't you have the name of the column you want it to be changed to listed in the DropDownList1 and then use DropDownList1.SelectedValue like so:
protected void Button1_Click(object sender, EventArgs e)
{
string conString = #"Data Source=FATTO-TOSH\SQLEXPRESS;Initial Catalog=Positions;Integrated Security=True";
using (var con = new SqlConnection(conString))
{
var cmd = new SqlCommand("sys.sp_rename", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#objname", DropDownList1.SelectedValue)
.SqlDbType = SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("#newname", name.Text)
.SqlDbType = SqlDbType.NVarChar;
cmd.ExecuteNonQuery();
}
}
MSDN
Make sure you toss in some null checking on the SelectedValue before you actually execute any commands.
I have a table of CheckBoxes that are inserted into a SQL db as '1' and '0'. However, I would like to retrieve those values again with a load event, but I'm not able to get them. This is my code:
private void getAuditChecklist()
{
SqlCommand cmd = null;
string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string queryString = #"SELECT Mount, Braker, Access, Conn_Net, Log_Book, Pictures, Floor, Cb_Lenght, Channel FROM AUDITOR_CHECKLIST " +
"WHERE SITE_ID = #SiteID";
using (SqlConnection connection =
new SqlConnection(conn))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
cmd = new SqlCommand(queryString);
cmd.Connection = connection;
cmd.Parameters.Add(new SqlParameter("#SiteID", //the name of the parameter to map
System.Data.SqlDbType.NVarChar, //SqlDbType value
20, //The width of the parameter
"SITE_ID")); //The name of the column source
//Fill the parameter with the value retrieved
//from the text field
cmd.Parameters["#SiteID"].Value = foo.Site_ID;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
CheckBox1.Checked = (reader.GetBoolean(reader.GetOrdinal("Mount")));
CheckBox2.Checked = (reader.GetBoolean(reader.GetOrdinal("Braker")));
CheckBox3.Checked = (reader.GetBoolean(reader.GetOrdinal("Access")));
CheckBox4.Checked = (reader.GetBoolean(reader.GetOrdinal("Conn_Net")));
CheckBox5.Checked = (reader.GetBoolean(reader.GetOrdinal("Log_Book")));
CheckBox6.Checked = (reader.GetBoolean(reader.GetOrdinal("Pictures")));
CheckBox8.Checked = (reader.GetBoolean(reader.GetOrdinal("Floor")));
CheckBox9.Checked = (reader.GetBoolean(reader.GetOrdinal("Cb_lenght")));
CheckBox10.Checked = (reader.GetBoolean(reader.GetOrdinal("Channel")));
}
reader.Close();
}
}
What am I missing to get the checkmark from the sql db? Below is how insert into sql:
private void SaveAuditChecklist()
{
if (auditChecklist != null)
{
SqlCommand cmd = null;
string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string queryString = #"INSERT INTO AUDITOR_CHECKLIST VALUES(" +
"#SiteID, #Mount, #Braker, #Access, #ConnNet, #LogBook, #Pictures, #Floor, #CbLenght, #Channel) ";
using (SqlConnection connection =
new SqlConnection(conn))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
cmd = new SqlCommand(queryString);
cmd.Connection = connection;
cmd.Parameters.Add(new SqlParameter(
"#SiteID", //the name of the parameter to map
System.Data.SqlDbType.NVarChar, //SqlDbType value
20, //The width of the parameter
"Site_ID")); //The name of the column source
//Fill the parameter with the value retrieved
//from the text field
cmd.Parameters["#SiteID"].Value = foo.Site_ID;
cmd.Parameters.Add(new SqlParameter("#Mount", SqlDbType.Bit));
cmd.Parameters["#Mount"].Value = CheckBox1.Checked;
cmd.Parameters.Add(new SqlParameter("#Braker", SqlDbType.Bit));
cmd.Parameters["#Braker"].Value = CheckBox2.Checked;
cmd.Parameters.Add(new SqlParameter("#Access", SqlDbType.Bit));
cmd.Parameters["#Access"].Value = CheckBox3.Checked;
cmd.Parameters.Add(new SqlParameter("#ConnNet", SqlDbType.Bit));
cmd.Parameters["#ConnNet"].Value = CheckBox4.Checked;
cmd.Parameters.Add(new SqlParameter("#LogBook", SqlDbType.Bit));
cmd.Parameters["#LogBook"].Value = CheckBox5.Checked;
cmd.Parameters.Add(new SqlParameter("#Pictures", SqlDbType.Bit));
cmd.Parameters["#Pictures"].Value = CheckBox6.Checked;
cmd.Parameters.Add(new SqlParameter("#Floor", SqlDbType.Bit));
cmd.Parameters["#Floor"].Value = CheckBox8.Checked;
cmd.Parameters.Add(new SqlParameter("#CbLenght", SqlDbType.Bit));
cmd.Parameters["#CbLenght"].Value = CheckBox9.Checked;
cmd.Parameters.Add(new SqlParameter("#Channel", SqlDbType.Bit));
cmd.Parameters["#Channel"].Value = CheckBox10.Checked;
cmd.ExecuteReader();
}
}
}
Booleans are stored as 1 or 0 in Sql Database, but the datareader do the conversion for you. Instead use:
var myBool = reader.GetBoolean(i);
Then just assign the value to the control's value property.
Finally got the reader they way it should work to get the checkmark values. I edited my question with the working code; however, below is what I added or changed for the reader:
while (reader.Read())
{
CheckBox1.Checked = (reader.GetBoolean(reader.GetOrdinal("Mount")));
CheckBox2.Checked = (reader.GetBoolean(reader.GetOrdinal("Braker")));
CheckBox3.Checked = (reader.GetBoolean(reader.GetOrdinal("Access")));
CheckBox4.Checked = (reader.GetBoolean(reader.GetOrdinal("Conn_Net")));
CheckBox5.Checked = (reader.GetBoolean(reader.GetOrdinal("Log_Book")));
CheckBox6.Checked = (reader.GetBoolean(reader.GetOrdinal("Pictures")));
CheckBox8.Checked = (reader.GetBoolean(reader.GetOrdinal("Floor")));
CheckBox9.Checked = (reader.GetBoolean(reader.GetOrdinal("Cb_lenght")));
CheckBox10.Checked = (reader.GetBoolean(reader.GetOrdinal("Channel")));
}