| Call a Sql Server Stored Procedure with in/out
parameters (C#) public void update_plans(string stateid,string
countyid,string pposubdesig,int ppo,int posdirect,int hmodirect,int
poselect,int hmoselect)
{
//TODO: Add this SP to the Implementation plan: SamUpdatePlans
try {
using (SqlConnection conn = new SqlConnection(test_conn))
{
SqlCommand spCMD = new SqlCommand("SamUpdatePlans", conn);
spCMD.CommandType = CommandType.StoredProcedure;
//Add the parameters
SqlParameter RetVal = spCMD.Parameters.Add
("RetVal", SqlDbType.Int);
RetVal.Direction = ParameterDirection.ReturnValue;
SqlParameter StateIn = spCMD.Parameters.Add
("@STATEID", SqlDbType.VarChar, 2);
StateIn.Direction = ParameterDirection.Input;
StateIn.Value = stateid;
SqlParameter CountyIn = spCMD.Parameters.Add
("@COUNTYID", SqlDbType.VarChar, 40);
CountyIn.Direction = ParameterDirection.Input;
CountyIn.Value = countyid;
SqlParameter pposubdesigin = spCMD.Parameters.Add
("@PPOSUBDESIG", SqlDbType.Text, 40);
pposubdesigin.Direction = ParameterDirection.Input;
pposubdesigin.Value = pposubdesig;
SqlParameter ppoin = spCMD.Parameters.Add
("@PPO", SqlDbType.TinyInt);
ppoin.Direction = ParameterDirection.Input;
ppoin.Value = ppo;
SqlParameter posdirectin = spCMD.Parameters.Add
("@POSDIRECT", SqlDbType.TinyInt);
posdirectin.Direction = ParameterDirection.Input;
posdirectin.Value = posdirect;
SqlParameter hmodirectin = spCMD.Parameters.Add
("@HMODIRECT", SqlDbType.TinyInt);
hmodirectin.Direction = ParameterDirection.Input;
hmodirectin.Value = hmodirect;
SqlParameter posselectin = spCMD.Parameters.Add
("@POSSELECT", SqlDbType.TinyInt);
posselectin.Direction = ParameterDirection.Input;
posselectin.Value = poselect;
SqlParameter hmoselectin = spCMD.Parameters.Add
("@HMOSELECT", SqlDbType.TinyInt);
hmoselectin.Direction = ParameterDirection.Input;
hmoselectin.Value = hmoselect;
conn.Open();
spCMD.ExecuteNonQuery();
conn.Close();
}
}
catch (Exception ex)
{
}
}
Populate a ComboBox with a comma delimited string from the app.config
file (C#)
public void populate_states()
{
//<add key="CURRENT_STATES" value="Select a State,HI,NC,VA,WI,WV" />
//put the current states into the states drop-down
try
{
//put the current states into the states drop-down
string states = ConfigurationManager.AppSettings["CURRENT_STATES"];
char[] delimitor = { ',' };
string[] delim_states = states.Split(delimitor);
for (int i = 0; i < delim_states.Length; i++)
{
string part = delim_states[i];
cbo_state.Items.Add(part);
}
cbo_state.SelectedIndex = 0;
}
catch (Exception )
{
throw;
}
}
Populate a Datareader with an Access Database call and change
control based on output (C#)
protected void btn_DictCheck_Click(object sender, EventArgs e)
{
try
{
//Placeholder for password lookup
String sPath = "C:\\DB\\pwd.mdb";
System.Data.OleDb.OleDbConnection con;
System.Data.OleDb.OleDbDataReader dr;
System.Data.OleDb.OleDbCommand cmd;
con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +
sPath;
con.Open();
cmd = new System.Data.OleDb.OleDbCommand("select word_entry from
unabridged_dictionary where word_entry ='" + this.txt_DICTPWD.Text +
"'", con);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
this.txt_DICTPWD.BorderStyle = BorderStyle.Dashed;
this.txt_DICTPWD.BorderColor = System.Drawing.Color.Red;
this.txt_DICTPWD.BorderWidth = 3;
con.Close();
dr.Close();
con.Dispose();
dr.Dispose();
}
else
{
this.txt_DICTPWD.BorderStyle = BorderStyle.Solid;
this.txt_DICTPWD.BorderColor = System.Drawing.Color.GreenYellow;
this.txt_DICTPWD.BorderWidth = 3;
con.Close();
dr.Close();
}
}
catch (Exception ex)
{
}
finally
{
}
}
Append or Create a file using the System.IO.StreamWriter and
System.IO.File Classes (C#)
//using System.IO;
public Boolean Write_Log_Entry(string Log_entry)
{
try
{
string sLogPath = ConfigurationManager.AppSettings["LOG_PATH"];
string sLogname = ConfigurationManager.AppSettings["LOG_NAME"];
string full_log_namePath = sLogPath + "\\" + sLogname;
// Self Explanatory
StreamWriter SW;
if (File.Exists(full_log_namePath))
{
//Append to the log file
SW = File.AppendText(full_log_namePath);
SW.WriteLine(Log_entry);
SW.Close();
return true;
}
else
{
// Make the file if it doesn't exist, then append to it
File.Create(full_log_namePath);
SW = File.AppendText(full_log_namePath);
SW.WriteLine(Log_entry);
SW.Close();
return false;
}
}
catch
{
MessageBox.Show("An error occurred. Can not access the application log",
"Application Error");
return false;
}
}
|