Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
string query = "insert into failure_report(job_number, Asset_no, component_des) VALUES (" + jobnumber.Text.ToString().Trim() + "," + AssetNumber.Text.ToString().Trim() + ",'" + compdes.Text.ToString().Trim() + "')";
string vJobNumber="";
string vAssetNumber="";
string vcompdes="";
if (jobnumber.SelectedIndex != -1)
{
vJobNumer = jobnumber.SelectedItem.ToString().Trim();
}
if (AssetNumber.SelectedIndex != -1)
{
vAssetNumber = AssetNumber.SelectedItem.ToString().Trim();
}
if (compdes.SelectedIndex != -1)
{
vcompdes = compdes.SelectedItem.ToString().Trim();
}
string query = "insert into failure_report(job_number, Asset_no, component_des) VALUES ('" + vjobnumber + "','" + vAssetNumber + "','" + vcompdes + "')";
SqlConnection oConn = new SqlConnection(strConnection);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("SELECT Code, Province FROM _tStates", oConn);
dt.Fill(dt);
//..
comboBox1.DataSource = dt;
comboBox1.DisplayMember="Province";
comboBox1.ValueMember ="Code";
private void InitCbx(ref System.Windows.Forms.ComboBox ctl, string strConnection, string txtSQL, string displayMember, string valMember)
{
SqlConnection oConn = null;
SqlCommand workingCommand = null;
SqlDataAdapter workingDataAdapter = null;
try
{
oConn = new SqlConnection(strConnection);
workingCommand = new SqlCommand();
workingDataAdapter = new SqlDataAdapter();
oConn.Open();
workingCommand.Connection = oConn;
workingCommand.CommandText = txtSQL;
workingDataAdapter.SelectCommand = workingCommand;
DataTable dtSource = new DataTable();
workingDataAdapter.Fill(dtSource);
ctl.DataSource = dtSource;
ctl.DisplayMember = displayMember;
ctl.ValueMember = valMember;
}
catch(Exception e)
{
string sMsg = "InitCbx(): " + e.GetType().Name + ":" + e.Message;
//_LogEvent(sMsg ,_enumLogType.Error,_enumLogDestination.LogFile,false);
}
finally
{
// Free memory
if (oConn !=null)
{
oConn.Close();
oConn.Dispose();
}
if (workingDataAdapter !=null)
workingDataAdapter.Dispose();
if (workingCommand !=null)
workingCommand.Dispose();
}
}
string strConnection="user id=xxxx;Password=blabla;Initial Catalog=mydb;Data Source=localhost;Connect Timeout=5";
string strSQL="SELECT Code, Province from _tStates";
InitCbx(ref comboBox1, strConnection,strSQL,"Province","");
strSQL = "SELECT AssetNumber from Asset";
InitCbx(ref comboBox2, strConnection,strSQL,"AssetNumber","");
strSQL = "SELECT jobnumber from Jobs";
InitCbx(ref comboBox3, strConnection,strSQL,"jobnumber","");
1. I created on my localhost SQL a table named SubAssembly with the following columns:
id -> int
name ->varchar 50
amount ->numeric
2. I added 3 rows
3. I created a form on which I put a ComboBox named SubAssembly and a buuton named btnInit.
4. On the btnInit_Click() handler I called the following method which contains your code but my connection string:
private void SubAssembly_Init()
{
//string strConnection = "Initial Catalog=EIS;Data Source=test;User ID=EIS;password=test;";
string strConnection = "Initial Catalog=EIS;Data Source=test;User ID=EIS;password=test;";
SqlConnection oConn = new SqlConnection(strConnection);
oConn.Open();
try
{
DataSet ds = new DataSet("test");
SqlDataAdapter da = new SqlDataAdapter("SELECT name FROM subassembly", oConn);
da.Fill(ds,"testda");
DataTable dt = ds.Tables["testda"];
SubAssembly.DataSource = dt;
SubAssembly.DisplayMember = "name";
}
catch(Exception ex)
{
MessageBox.Show("The error is : " + ex.Message + " , The stack trace is : " + ex.StackTrace + " , The Source is : " + ex.Source );
}
oConn.Close();
}
private void btnInit_Click(object sender, System.EventArgs e)
{
SubAssembly_Init();
}