Issues with Repeater going through each item - c#

I am having issues with looping through my repeater to save all the data. If I have 30 items in the repeater it will go through the repeater 30 times. However the ID never is updated. I set the id to textbox so I know the value is correct with DB and not duplicated.
Here i the code.
protected void Save()
{
foreach (RepeaterItem RI in Repeater1.Items)
{
bool budgetvalue = CBBudgeted.Checked;
//lblHeader.Text = rs["BudgetYear"].ToString();
string Janbudget = txtJanBudget.Text;
string Febbudget = txtFebBudget.Text;
string Marbudget = txtMarBudget.Text;
string Aprbudget = txtAprBudget.Text;
string Maybudget = txtMayBudget.Text;
string Junbudget = txtJunBudget.Text;
string Julbudget = txtJulBudget.Text;
string Augbudget = txtAugBudget.Text;
string Sepbudget = txtSepBudget.Text;
string Octbudget = txtOctBudget.Text;
string Novbudget = txtNovBudget.Text;
string Decbudget = txtDecBudget.Text;
string PriceEach = txtPriceEach.Text;
string IDnum = txtID.Text;
string saveStaff = "Update sales_budget " +
"set Janbudget " + Janbudget +
", Febbudget " + Febbudget +
", Marbudget " + Marbudget +
", Aprbudget " + Aprbudget +
", Maybudget " + Maybudget +
", Junbudget " + Junbudget +
", Julbudget " + Julbudget +
", Augbudget " + Augbudget +
", Sepbudget " + Sepbudget +
", Octbudget " + Octbudget +
", Novbudget " + Novbudget +
", Decbudget " + Decbudget +
", Budget " + budgetvalue +
", Price_Each " + PriceEach +
" Where id = " + IDnum;
GetConnection Sales = new GetConnection();
string conn = Sales.SalesConnect;
SqlCommand querySaveStaff = new SqlCommand(saveStaff);
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conn))
{
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(saveStaff, con);
con.Open();
try
{
command.ExecuteNonQuery();
}
catch
{
//GetConnection Sales = new GetConnection();
// string conn = Sales.SalesConnect;
// using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conn))
// {
// System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(mysql, con);
// con.Open();
// System.Data.SqlClient.SqlDataReader rs = command.ExecuteReader();
// while (rs.Read())
}
con.Close();
}
}

Related

else condition not working when reader hasrows is false

Can somebody tell me why the else condition is not working in the code below.
The link button in asp.net web application has following code in code behind: a parameterized SqlCommand fetch a row from a SQL Server database, the SqlDataReader rdr1.HasRows in if condition is working fine but else condition did not work.
Code updated
protected void LinkButton1_Click(object sender, EventArgs e)
{
string comid = DropDownList4.SelectedValue.ToString();
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("Select * from Commercials Where id =" + comid, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string dur = rdr["duration"].ToString();
Char delimiter = '/';
string[] dd = DateTime.Parse(rdr["rodate"].ToString()).ToString("dd/MM/yyyy").Split(delimiter);
if (DropDownList3.SelectedValue.ToString().Contains("BOL NEWS") == true && DropDownList1.SelectedValue.ToString().Contains("After Headlines") == true)
{
SqlConnection con0 = new SqlConnection(cs);
string sql01 = "Select * from CTS Where air_time=(Select max(air_time) from CTS where air_date=#airdate and air_time Like #airtime and channel=#channel and Slot=#slot) and air_date=#airdate1";
con0.Open();
SqlCommand cmd1 = new SqlCommand(sql01, con0);
cmd1.Parameters.AddWithValue("#airdate", TextBox1.Text);
cmd1.Parameters.AddWithValue("#channel", DropDownList3.SelectedValue.ToString());
cmd1.Parameters.AddWithValue("#airtime", DropDownList2.SelectedValue.ToString().Substring(0, 2) + "%");
cmd1.Parameters.AddWithValue("#slot", DropDownList1.SelectedValue.ToString().Remove(0, 3));
cmd1.Parameters.AddWithValue("#airdate1", TextBox1.Text);
SqlDataReader rdr1 = cmd1.ExecuteReader();
while (rdr1.Read())
{
string startTime0 = rdr1["air_time"].ToString();
string addsec = rdr1["duration"].ToString();
if (rdr1.HasRows)
{
DateTime startTime1 = DateTime.ParseExact(startTime0, "HH:mm:ss", null);
string startHeadlines_ = startTime1.AddSeconds(int.Parse(addsec)).ToString("HH:mm:ss");
using (SqlConnection con2 = new SqlConnection(cs))
{
string type = "Commercial";
string year = dd[2].ToString().Substring(dd[2].ToString().Length - 2);
string HouseId = "CH1COM001" + rdr["rono"] + rdr["duration"] + "S" + dd[1] + dd[0] + year;
string sql1 = "Insert into CTS(air_date,air_time,HouseNumber,rono,Title,duration,Slot,type,channel)Values('" + TextBox1.Text + "','" + startHeadlines_ + "','" + HouseId + "','" + rdr["rono"] + "','" + rdr["slug"] + "','" + rdr["duration"] + "','" + DropDownList1.SelectedValue.Remove(0, 3) + "','" + type + "','" + DropDownList3.SelectedValue.ToString() + "')";
con2.Open();
SqlCommand InsertCmd = new SqlCommand(sql1, con2);
InsertCmd.ExecuteNonQuery();
con2.Close();
}
}
else
{
DateTime startTime = DateTime.ParseExact(DropDownList2.SelectedValue.ToString(), "HH:mm:ss", null);
string startHeadlines = startTime.AddSeconds(210).ToString("HH:mm:ss");
using (SqlConnection con1 = new SqlConnection(cs))
{
string type = "Commercial";
string year = dd[2].ToString().Substring(dd[2].ToString().Length - 2);
string HouseId = "CH1COM001" + rdr["rono"] + rdr["duration"] + "S" + dd[1] + dd[0] + year;
string sql = "Insert into CTS(air_date,air_time,HouseNumber,rono,Title,duration,Slot,type,channel)Values('" + TextBox1.Text + "','" + startHeadlines + "','" + HouseId + "','" + rdr["rono"] + "','" + rdr["slug"] + "','" + rdr["duration"] + "','" + DropDownList1.SelectedValue.ToString().Remove(0, 3) + "','" + type + "','" + DropDownList3.SelectedValue.ToString() + "')";
con1.Open();
SqlCommand InsertCmd = new SqlCommand(sql, con1);
InsertCmd.ExecuteNonQuery();
con1.Close();
}
}
}
con0.Close();
}
}
con.Close();
}
}

get row with same id but different value and display different value into different label asp.net

this work but i want to display if value exist, so user can see the value username and last_update in each label based on each process_id but they have same request_id. this code only display username and last_update for the one that based on username login only.how i can do that ?
using(SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["MESTrans"].ConnectionString))
{
//bool exists = false;
int exists;
con.Open();
foreach (GridViewRow row in GridView2.Rows)
{
int req_id = Convert.ToInt32(GridView2.DataKeys[row.RowIndex].Values[0]);
using (SqlCommand cmd = new SqlCommand("select count(*) from [CutPanelConfirmation] where req_id=#req_id and process_id=#process_id", con))
{
cmd.Parameters.AddWithValue("#process_id", Session["process_id"].ToString());
cmd.Parameters.AddWithValue("#req_id", req_id);
exists = (int)cmd.ExecuteScalar();
}
if (exists > 0)
{
query = "select * from CutPanelConfirmation where req_id=#req_id and process_id=#process_id";
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("#process_id", Session["process_id"].ToString());
cmd.Parameters.AddWithValue("#req_id", req_id);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if ((string)dr["process_id"] == "marker")
{
LabelNameMarker.Text = LabelNameMarker.Text + " " + dr[1].ToString();
LabelDateMarker.Text = LabelDateMarker.Text + " " + dr[3].ToString();
}
if ((string)dr["process_id"] == "fabric")
{
LabelNameFabric.Text = LabelNameFabric.Text + " " + dr[1].ToString();
LabelDateFabric.Text = LabelDateFabric.Text + " " + dr[3].ToString();
}
if ((string)dr["process_id"] == "cutting")
{
LabelNameCutting.Text = LabelNameCutting.Text + " " + dr[1].ToString();
LabelDateCutting.Text = LabelDateCutting.Text + " " + dr[3].ToString();
}
if ((string)dr["process_id"] == "SPP")
{
LabelNameSPP.Text = LabelNameSPP.Text + " " + dr[1].ToString();
LabelDateSPP.Text = LabelDateSPP.Text + " " + dr[3].ToString();
}
if ((string)dr["process_id"] == "kitting")
{
LabelNameKitting.Text = LabelNameKitting.Text + " " + dr[1].ToString();
LabelDateKitting.Text = LabelDateKitting.Text + " " + dr[3].ToString();
}
if ((string)dr["process_id"] == "sew")
{
LabelNameSew.Text = LabelNameSew.Text + " " + dr[1].ToString();
LabelDateSew.Text = LabelDateSew.Text + " " + dr[3].ToString();
}
}
dr.Close();
}
con.Close();
}
}
posting [https://stackoverflow.com/users/7892938/nfh][NFH] answer
i just delete#process id from this query and it works as what i want! :
query = "select * from CutPanelConfirmation where req_id=#req_id and process_id=#process_id"; cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("#process_id", Session["process_id"].ToString()); cmd.Parameters.AddWithValue("#req_id", req_id); SqlDataReader dr = cmd.ExecuteReader();

SqlDataReader returns x rows but SQL query returns y rows

First time post as I'm a bit stuck here.
I am using this code to return some rows from a SQL Server database:
public static SqlDataReader SQLSelect(string sqlcommand, string[,] parameters, int length)
{
try
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
conn.Open();
SqlDataReader reader;
SqlCommand cmd = new SqlCommand(sqlcommand, conn);
var allLength = parameters.Length;
for (int i = 0; i < parameters.Length - length; i++)
{
string paramid = parameters[i, 0];
if (paramid == "#date" || paramid == "#Date" || paramid == "#DATE")
{
string paramvalue = parameters[i, 1];
DateTime date = Convert.ToDateTime(paramvalue);
paramvalue = date.ToString("yyyy-MM-dd HH':'mm':'ss");
cmd.Parameters.Add(new SqlParameter(paramid, paramvalue));
}
else
{
string paramvalue = parameters[i, 1];
cmd.Parameters.Add(new SqlParameter(paramid, paramvalue));
}
}
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();
return reader;
}
catch
{
return null;
}
}
This function is called like so
string[,] parameters = new string[1, 2] { { "#studentid", studentid } };
SqlDataReader reader = Common.SQLSelect(Common.tblstudentprogressselectforprinting, parameters, 1);
now all runs fine except the reader only contains 13 rows of data where as the actual query being
exec sp_tblstudentprogress_selectforprinting #studentid=N'87'
as an example, returns 91 rows.
I'm at a loss as to why this is the case. Only thing I have noticed is when using SQL Server profiler, running the query from SQL Server, there is a RPC: Started and Completed, as for running from withing my web app, there is only an RPC: Started.
Any thoughts on this?
EDIT:
here is how I enumerate the reader
protected void btnPrint_Click(object sender, EventArgs e)
{
string[,] parameters = new string[1, 2] { { "#studentid", studentid } };
SqlDataReader reader = Common.SQLSelect(Common.tblstudentprogressselectforprinting, parameters, 1);
string firstname = txtFirstName.Text;
string lastname = txtLastName.Text;
int i=0;
string[] heading1 = new string[reader.FieldCount];
string[] heading2 = new string[reader.FieldCount];
string[] log = new string[reader.FieldCount];
try
{
while (reader.Read())
{
heading1[i] = "Progress Log for: Block: " + reader["block"].ToString() + " Lesson: " + reader["lesson"].ToString();
heading2[i] = "";
log[i] =
/*"PROGRESS LOG for " + reader["firstname"].ToString() + " " + reader["lastname"].ToString() + " Printed on " + DateTime.Today.ToShortDateString() + Environment.NewLine +*/
Environment.NewLine +
"Teacher: " + reader["teacher"].ToString() + Environment.NewLine +
"Date: " + reader["date"].ToString() + Environment.NewLine +
"Year: " + reader["year"].ToString() + Environment.NewLine +
"Block: " + reader["block"].ToString() + Environment.NewLine +
"Lesson: " + reader["lesson"].ToString() + Environment.NewLine +
"Warm Up: " + reader["warmup"].ToString() + Environment.NewLine +
"Range: " + reader["range"].ToString() + Environment.NewLine +
"Technique Sheet: " + reader["techniquesheet"].ToString() + Environment.NewLine +
"Technique Other: " + reader["techniqueother"].ToString() + Environment.NewLine +
Environment.NewLine +
"Notes: " + reader["notes"].ToString() + Environment.NewLine +
Environment.NewLine +
"Mark: " + reader["mark"].ToString()+ Environment.NewLine ;
i++;
}
}
catch
{
}
finally
{
if (Common.conn != null)
{
Common.conn.Close();
}
}
Common.PDFCreateProgressLog("Progress log for: " + firstname + " " + lastname, "Progress log for: " + firstname + " " + lastname, "PDF_" + firstname + " " + lastname + "-" + DateTime.Today.ToString("yyyy-MM-dd") + ".pdf", "Progress log for: " + firstname + " " + lastname, log, heading1, heading2);
}
You are confusing the meaning of the FieldCount property. It identifies the number of Columns, not the number of Rows. You cannot determine the total number of rows from a streaming source like a Reader, without enumerating all of the rows first (at least once, anyway).
So you will need to extend your arrays each time (lists might be easier for this) you read a row from the Reader and test the Reader to se when there are no more rows.

code ignored after first button click

This button (loginButton) does everything it's supposed to on the first click. Then, on all subsequent clicks, it refuses change the text of loginStatusLabel at the beginning and end of the method...why? It's as if the code is completely skipped over for with no explanation. The debug messages before and afterword both come through fine, and loginStatusLabel isn't referenced anywhere else.
Am I an idiot?
private void loginButton_Click(object sender, EventArgs e)
{
Debug.Write("Changing label...");
loginStatusLabel.Text = "STATUS: Running..."; //this line is ignored after the first iteration
Debug.WriteLine("label changed.");
loginButton.Enabled = false;
try
{
string LDS01_start = "select count(*) from BBLEARN.AUTH_PROVIDER_LOG where AUTH_PROVIDER_PK1 = 103 ";
string LDAPS_start = "select count(*) from BBLEARN.AUTH_PROVIDER_LOG where AUTH_PROVIDER_PK1 = 106 ";
string middle = "and log_date >= '" + GetDate(loginStartDate) + #"'
and log_date < '" + GetDate(loginEndDate) + #"' ";
string LDS01_0 = LDS01_start + middle + "and event_type = 0";
string LDS01_1 = LDS01_start + middle + "and event_type = 1";
string LDS01_2 = LDS01_start + middle + "and event_type = 2";
string LDS01_5 = LDS01_start + middle + "and event_type = 5";
string LDS01_6 = LDS01_start + middle + "and event_type = 6";
string LDAPS_0 = LDAPS_start + middle + "and event_type = 0";
string LDAPS_1 = LDAPS_start + middle + "and event_type = 1";
string LDAPS_2 = LDAPS_start + middle + "and event_type = 2";
string LDAPS_5 = LDAPS_start + middle + "and event_type = 5";
string LDAPS_6 = LDAPS_start + middle + "and event_type = 6";
GetData(LDS01_0, LDS01_LB0);
GetData(LDS01_1, LDS01_LB1);
GetData(LDS01_2, LDS01_LB2);
GetData(LDS01_5, LDS01_LB5);
GetData(LDS01_6, LDS01_LB6);
GetData(LDAPS_0, LDAPS_LB0);
GetData(LDAPS_1, LDAPS_LB1);
GetData(LDAPS_2, LDAPS_LB2);
GetData(LDAPS_5, LDAPS_LB5);
GetData(LDAPS_6, LDAPS_LB6);
}
catch (Exception exception)
{
Debug.WriteLine(exception);
}
loginButton.Enabled = true;
Debug.Write("Changing label...");
loginStatusLabel.Text = "STATUS: Complete";
Debug.WriteLine("label changed.");
}
private void GetData(string selectCommand, Label label)
{
Debug.WriteLine("Getting data for " + label.Name + "...");
//open the connection
OracleConnection conn = new OracleConnection(connectString);
conn.Open();
Debug.WriteLine("Connection open...");
//define the command
selectCommand = selectCommand.Replace(Environment.NewLine, " ");
OracleDataAdapter dataAdapter = new OracleDataAdapter(selectCommand, conn);
OracleCommandBuilder commandBuilder = new OracleCommandBuilder(dataAdapter);
//run the command
Debug.WriteLine("Running command...");
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
Debug.WriteLine("Command complete.");
//close the connection
conn.Close();
Invoke(new Action(() => RenderData(label, table.Rows[0][0].ToString())));
}
private void RenderData(Label label, string text)
{
label.Text = text;
}
Forgot to create the new thread in the first place. I'm stupid.

EMPLOYEE MDB ACCESS GET ERROR 'Message: No value given for one or more required parameters'

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string gs_ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=YorkProducts.accdb;"
+ "Persist Security Info=False;";
public Form1()
{
InitializeComponent();
}
private void ClearForm()
{
tb_EmpNr.Clear();
tb_LastName.Clear();
tb_FirstName.Clear();
tb_MiddleName.Clear();
tb_Address.Clear();
tb_Zip.Clear();
tb_City.Clear();
tb_State.Clear();
tb_Phone.Clear();
tb_PayLevel.Clear();
tb_PayGrade.Clear();
tb_DOH.Clear();
}//end clear form
private void GetCityState()
{
try
{
//create the connection
string Query = "SELECT City, State " +
" FROM tbl_Zip WHERE Zip=" + "\"" + tb_Zip.Text + "\"" + "";
OleDbConnection Connection = new OleDbConnection(gs_ConnString);
OleDbCommand Command = new OleDbCommand(Query, Connection);
Connection.Open();
OleDbDataReader Reader;
Reader = Command.ExecuteReader();
Reader.Read();
tb_City.Text = Reader.GetString(0);
tb_State.Text = Reader.GetString(1);
Reader.Close();
Connection.Close();
}//end try
catch (Exception error)
{
MessageBox.Show("Message: " + error.Message,
"EMPLOYEE MDB ACCESS GET ZIP ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}//end catch
}//end get city state
private void GetRecFromDatabase(string s_EmpNr)
{
try
{
//create the connection
string Query = "SELECT EmpNumber, EmpLastName, EmpFirstName, EmpMiddleName, EmpAddress, EmpZip," +
" EmpPhone, EmpPayLevel, EmpPayGrade, EmpHireDate" +
" FROM tbl_Employee WHERE EmpNumber=" + "\"" + s_EmpNr + "\"" + "";
OleDbConnection Connection = new OleDbConnection(gs_ConnString);
OleDbCommand Command = new OleDbCommand(Query, Connection);
Connection.Open();
OleDbDataReader Reader;
Reader = Command.ExecuteReader();
Reader.Read();
tb_EmpNr.Text = Reader.GetString(0);
tb_LastName.Text = Reader.GetString(1);
tb_FirstName.Text = Reader.GetString(2);
tb_MiddleName.Text = Reader.GetString(3);
tb_Address.Text = Reader.GetString(4);
tb_Zip.Text = Reader.GetString(5);
tb_Phone.Text = Reader.GetString(6);
tb_PayGrade.Text = String.Format("{0}", Reader.GetInt32(7));
tb_PayLevel.Text = String.Format("{0}", Reader.GetInt32(8));
tb_DOH.Text = String.Format("{0:d}", Reader.GetDateTime(9));
Reader.Close();
Connection.Close();
GetCityState();
}//end try
catch (Exception error)
{
MessageBox.Show("Message: " + error.Message,
"EMPLOYEE MDB ACCESS GET ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}//end catch
}//end GetRecFromDatabase
private void b_Clear_Screen_Click_Click(object sender, EventArgs e)
{
ClearForm();
tb_EmpNr.Focus();
}
private void b_Add_Customer_Click(object sender, EventArgs e)
{
try
{
//create the connection
string Query = "INSERT INTO tbl_Employee (EmpNumber, EmpLastName, EmpFirstName, EmpMiddleName, " +
"EmpAddress, EmpZip, EmpPhone, EmpHireDate, EmpPayLevel, EmpPayGrade)" +
"VALUES(" + "\"" + tb_EmpNr.Text + "\"" + ", " //strings get double quotes
+ "\"" + tb_LastName.Text + "\"" + ", "
+ "\"" + tb_FirstName.Text + "\"" + ", "
+ "\"" + tb_MiddleName.Text + "\"" + ", "
+ "\"" + tb_Address.Text + "\"" + ", "
+ "\"" + tb_Zip.Text + "\"" + ", "
+ "\"" + tb_Phone.Text + "\"" + ", "
+ "#" + tb_DOH.Text + "#" + ", "
+ tb_PayLevel.Text + ", "
+ tb_PayGrade.Text
+ ")";
OleDbConnection Connection = new OleDbConnection(gs_ConnString);
OleDbCommand Command = new OleDbCommand(Query, Connection);
Connection.Open();
OleDbDataReader Reader;
Reader = Command.ExecuteReader();
Reader.Close();
Connection.Close();
}//end try
catch (Exception error)
{
MessageBox.Show("Message: " + error.Message,
"EMPLOYEE MDB ACCESS ADD ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}//end catch
}
private void b_Get_Customer_Click(object sender, EventArgs e)
{//get a specific rec
GetRecFromDatabase(tb_EmpNr.Text);
}
private void tb_Zip_TextChanged(object sender, EventArgs e)
{//get the city and state
GetCityState();
}
private void b_Get_Next_Customer_Click(object sender, EventArgs e)
{//get next rec
string s_NextRec = Convert.ToString(Convert.ToInt32(tb_EmpNr.Text) + 1);
GetRecFromDatabase(s_NextRec);
}
private void b_Update_Customer_Click(object sender, EventArgs e)
{//update the record
try
{
string Query = "UPDATE tbl_Employee SET " +
"EmpLastName =" + "\"" + tb_LastName.Text + "\"" + ", " +
"EmpFirstName =" + "\"" + tb_FirstName.Text + "\"" + ", " +
"EmpMiddleName =" + "\"" + tb_MiddleName.Text + "\"" + ", " +
"EmpAddress =" + "\"" + tb_Address.Text + "\"" + ", " +
"EmpZip =" + "\"" + tb_Zip.Text + "\"" + ", " +
"EmpPhone =" + "\"" + tb_Phone.Text + "\"" + ", " +
"EmpHireDate =" + "#" + tb_DOH.Text + "#" + " " + ", " +
"EmpPayLevel =" + Convert.ToInt32(tb_PayLevel.Text) + ", " +
"EmpPayGrade =" + Convert.ToInt32(tb_PayGrade.Text) +
" WHERE EmpNumber =" + "\"" + tb_EmpNr.Text + "\"" + " ";
OleDbConnection Connection = new OleDbConnection(gs_ConnString);
OleDbCommand Command = new OleDbCommand(Query, Connection);
Connection.Open();
OleDbDataReader Reader;
Reader = Command.ExecuteReader();
Reader.Close();
Connection.Close();
GetRecFromDatabase(tb_EmpNr.Text);
}//end try
catch (Exception error)
{
MessageBox.Show("Message: " + error.Message,
"EMPLOYEE MDB ACCESS ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}//end catch
}//end update click
}//end class
}//end namespace
This program is supposed to grab data from an access database that I have manually created and place the data into the fields of a form. I have placed the access database into the debug folder of the project(in visual studio) however I am receiving the error message stated in the title. I think it is possible that I have the database file in the wrong location? I have added Microsoft Access 14.0 as a reference and installed the Microsoft Access Database engine for my version of access. Any help would be appreciated! Thanks!
Solution 1:
string Query = "SELECT City, State FROM tbl_Zip WHERE Zip='" + tb_Zip.Text+"'";
Solution 2:
//create the connection
string Query = "SELECT City, State FROM tbl_Zip WHERE Zip=#zip";
OleDbConnection Connection = new OleDbConnection(gs_ConnString);
OleDbCommand Command = new OleDbCommand(Query, Connection);
Command.Parameters.AddWithValue("#zip",tb_Zip.Text);
Connection.Open();
OleDbDataReader Reader;
Reader = Command.ExecuteReader();

Categories

Resources