cannot bind on a new display member combobox c# - winforms

When I run my program on my machine it works totally fine but when I run the code from other machine, I get an Error that says "Cannot bind on a new display member"? I just don't understand why? Here is my code.
private DataTable effectivity, filesTable, cgmFiles;
private void FillEffectivity()
{
cbEffectivity.ComboBox.DataSource = null;
effectivity = FillDataTable("SELECT * FROM Projects ORDER BY Project ASC");
cbEffectivity.ComboBox.DataSource = effectivity;
cbEffectivity.ComboBox.DisplayMember = "project";
cbEffectivity.ComboBox.ValueMember = "RecNo";
}
private DataTable FillTable(string sql)
{
DataTable table = new DataTable();
try
{
using (OleDbDataAdapter da = new OleDbDataAdapter(sql, cn))
{
da.Fill(table);
}
}
catch (Exception ex)
{
if (ex.Message == "Not a valid password.")
{
SetPassword();
return FillTable(sql);
}
return new DataTable();
}
finally
{
cn.Close();
}
return table;
}

It seems like problem in your query instead of effectivity = FillDataTable("SELECT * FROM Projects ORDER BY Project ASC"); try FillDataTable("SELECT * FROM Projects ORDER BY project ASC");
also it is recommended that dont use * while selecting it will affect on performance.
try this
FillDataTable("SELECT project FROM Projects ORDER BY project ASC");

Related

SSIS script component as source Null value

I have package in SSIS that loads data using adomd.Net from essbase cube. With 2 columns package works fine, but when I add 3. column package fails.
Error message says:
Object reference not set to an instance of an object.
Third column in the first row contains null and some values in other rows.
Problem looks in null value in the 3. column.
I tried this if statement but I get empty column instead of values.
AdomdDataReader reader = null;
try
{
using (AdomdConnection conn = new AdomdConnection(connectionString))
{
conn.Open();
using (AdomdCommand cmd = new AdomdCommand(query, conn))
{
reader = cmd.ExecuteReader();
while (reader.Read())
{
Output0Buffer.AddRow();
Output0Buffer.Column = (reader.GetString(0));
Output0Buffer.Column1 = (reader.GetString(1));
if (!reader.IsDBNull(2))
{
Output0Buffer.Column2 = "test";
}
else
{
Output0Buffer.Column2 = (reader.GetString(2));
}
Console.WriteLine("fin");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
This bit of the code is working the opposite way as intended:
if (!reader.IsDBNull(2))
{
Output0Buffer.Column2 = "test";
}
else
{
Output0Buffer.Column2 = (reader.GetString(2));
}
!reader.IsDBNull(2) means where the value is not null. Removing the exclamation point will fix the problem

How to run ms access database in jar using java 8 ucanaccess

I have develop a program in java using eclipse, its a JTable that would connect to my ms access database, all of the data from my ms access table will be shown in my JTable in a click of a button. My program works fine every time i run it in eclipse. My problem is that, when i export it and make a runnable jar file and click that created jar file this message appears:
java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://SUGGESTION_SOURCE_FODEL/suggestionTableFinal.mdb
SUGGESTION_SOURCE_FODEL is the folder where my ms access database table is located and suggestionTableFinal is my ms access datbase table. Any help would be much appreciated! My code are as follow:
public void()
{
try
{
reloadDataSuggestion();
model = new DefaultTableModel(data,columnNames);
mainJTableSuggestion.setModel(model);
Dimension tableSize = mainJTableSuggestion.getPreferredSize();
mainJTableSuggestion.getColumn("SUGGESTIONS").setPreferredWidth(Math.round((tableSize.width - 220)* 1.20f));
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
mainJTableSuggestion.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
mainJTableSuggestion.setRowSorter(sorter);
st1.close();
rs1.close();
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(null, e1 + "This is my Error");
}
}
public void reloadDataSuggestion() throws ClassNotFoundException, SQLException
{
try
{
columnNames.clear();
data.clear();
String DBPAD = "SUGGESTION_SOURCE_FODEL/suggestionTableFinal.mdb";
String DB = "jdbc:ucanaccess://" + DBPAD;
con1 = DriverManager.getConnection(DB, "", "");
st1 = con1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = ("SELECT * FROM suggestionTableFinal");
rs1 = st1.executeQuery(sql);
ResultSetMetaData rsmd = rs1.getMetaData();
int column = rsmd.getColumnCount();
columnNames.addElement("Submitted");
columnNames.addElement("ClientNames");
columnNames.addElement("SUGGESTIONS");
columnNames.addElement("DateActed");
columnNames.addElement("Number");
columnNames.addElement("SuggestionAction");
while(rs1.next())
{
Vector<Object> row = new Vector<Object>(column);
for(int i=1; i<=column; i++)
{
row.addElement(rs1.getObject(i));
}
data.addElement(row);
}
showDataToTextFieldsFromJTableConnectedToDataBase1();
rs1.close();
st1.close();
totalNumber = 0;
totalNumber1 = 0;
}
catch(Exception e1)
{
final JOptionPane pane = new JOptionPane("<html>" +"<font color=\"#FF0000\">" + "<b><html><span style='font-size:1.2em'>" + e1);
final JDialog d = pane.createDialog((JFrame)null, "TitleFinal1");
d.setLocationRelativeTo(null);
d.setAlwaysOnTop(true);
d.setVisible(true);
}
}

Would singleton database connection affect performance in a weblogic clustered environment?

I have a Java EE struts web application using a singleton database connection. In the past, there is only one weblogic server, but now, there are two weblogic servers in a cluster.
Session replication have been tested to be working in this cluster. The web application consist of a few links that will open up different forms for the user to fill in. Each form has a dynamic dropdownlist that will populate some values depending on which form is clicked. These dropdownlist values are retrieved from the oracle database.
One unique issue is that the first form that is clicked, might took around 2-5 seconds, and the second form clicked could take forever to load or more than 5 mins. I have checked the codes and happened to know that the issue lies when an attempt to call the one instance of the db connection. Could this be a deadlock?
public static synchronized DataSingleton getDataSingleton()
throws ApplicationException {
if (myDataSingleton == null) {
myDataSingleton = new DataSingleton();
}
return myDataSingleton;
}
Any help in explaining such a scenario would be appreciated.
Thank you
A sample read operation calling Singleton
String sql = "...";
DataSingleton myDataSingleton = DataSingleton.getDataSingleton();
conn = myDataSingleton.getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
try {
pstmt.setString(1, userId);
ResultSet rs = pstmt.executeQuery();
try {
while (rs.next()) {
String group = rs.getString("mygroup");
}
} catch (SQLException rsEx) {
throw rsEx;
} finally {
rs.close();
}
} catch (SQLException psEx) {
throw psEx;
} finally {
pstmt.close();
}
} catch (SQLException connEx) {
throw connEx;
} finally {
conn.close();
}
The Singleton class
/**
* Private Constructor looking up for Server's Datasource through JNDI
*/
private DataSingleton() throws ApplicationException {
try {
Context ctx = new InitialContext();
SystemConstant mySystemConstant = SystemConstant
.getSystemConstant();
String fullJndiPath = mySystemConstant.getFullJndiPath();
ds = (DataSource) ctx.lookup(fullJndiPath);
} catch (NamingException ne) {
throw new ApplicationException(ne);
}
}
/**
* Singleton: To obtain only 1 instance throughout the system
*
* #return DataSingleton
*/
public static synchronized DataSingleton getDataSingleton()
throws ApplicationException {
if (myDataSingleton == null) {
myDataSingleton = new DataSingleton();
}
return myDataSingleton;
}
/**
* Fetching SQL Connection through Datasource
*
*/
public Connection getConnection() throws ApplicationException {
Connection conn = null;
try {
if (ds == null) {
}
conn = ds.getConnection();
} catch (SQLException sqlE) {
throw new ApplicationException(sqlE);
}
return conn;
}
It sounds like you may not be committing the transaction at the end of your use of the connection.
What's in DataSingleton - is it a database connection? Allowing multiple threads to access the same database connection is not going to work, for example once you have more than one user. Why don't you use a database connection pool, for example a DataSource?

DataGridView displaying superfluous column on the left - how to get rid of it?

My DataGridView displays a "0" column with a delta in the first row and a * in the second. I don't want these. They take up too much space. Is there a way to get them to go away?
I don't think the code is pertinent in this case, but to humor y'all, here it is:
private void PopulateLanguageAndPrimary() {
string query;
OracleDataAdapter da;
OracleDataTable dt;
OracleCommand oc;
try {
oracleConnectionMainForm.Open();
query = "select position, coach from players where team = :team";
da = new OracleDataAdapter();
oc = new OracleCommand(query, oracleConnectionMainForm);
oc.Parameters.Add("team", greenBayPackers);
da.SelectCommand = oc;
dt = new OracleDataTable();
da.Fill(dt);
dataGridViewLanguageAndPrimary.DataSource = dt;
} catch (OracleException ex) {
MessageBox.Show(ex.Message);
} finally {
oracleConnectionMainForm.Close();
}
}
added scream shot at: http://warbler.posterous.com/heres-the-unwanted-bling-on-the-datagridview#
Try setting dataGridViewLanguageAndPrimary.AllowUserToAddRow to false. This will remove the *. Then set RowHeadersVisible to false, hiding the superfluous column.

Windows Mobile C# application with database

I am writing a prototype application in Windows Mobile 6.5 device.
The objective of the app is to ask user for some inputs, collect data and store into local database and on a server.
I am done with creating GUI (in C#) of the application which takes all the necessary inputs from user.
Now, I need to insert this data into local DB and upload to server DB. Both the DBs will need to synced over HTTP when user selects to do so. I have not worked on databases much, except for writing some queries to fetch data from PostgreSQL in the past in Linux environment a few years ago.
So my question is, what is the easiest way to achieve the thing I am trying to? I don't need lot of features. The data is only strings and numbers (no files, multimedia stuff etc.)
What server I should install and run? What components should I use on client side?
Thanks
Ashish
To use database on windows mobile you need Microsoft SQL Server Compact 3.5 for Windows Mobile . http://www.microsoft.com/en-in/download/details.aspx?id=8831 . You can download and install from the link given. After installation C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Devices\wce500\armv4i will have all CAB files that needs to be installed to your mobile.
Install
sqlce.ppc.wce5.armv4i.CAB
sqlce.repl.ppc.wce5.armv4i.CAB
For more information on what to install refer http://msdn.microsoft.com/en-us/library/bb986876.aspx
I have written a small helper class to do all database transactions.
public class DataBaseHelper
{
public enum typeOfQuery
{
insert,
update,
delete,
getScalar,
getDataSet,
getDataTable
};
private string connectionString = Program.Connection;
public object ExecuteDatabaseQuery(string query, Dictionary<string, object> dictionary, typeOfQuery typeOfQuery)
{
try
{
using (SqlCeConnection oCon = new SqlCeConnection(connectionString))
{
oCon.Open();
string oSql = query;
using (SqlCeCommand oCmd = new SqlCeCommand(oSql, oCon))
{
oCmd.CommandType = CommandType.Text;
if (dictionary != null)
{
if (dictionary.Count != 0)
{
foreach (KeyValuePair<string, object> pair in dictionary)
{
if (pair.Value is DateTime)
oCmd.Parameters.Add(pair.Key, SqlDbType.DateTime).Value = pair.Value ?? DBNull.Value;
else if (pair.Value is bool || pair.Value is Boolean)
oCmd.Parameters.Add(pair.Key, SqlDbType.Bit).Value = pair.Value ?? DBNull.Value;
else
oCmd.Parameters.Add(pair.Key, SqlDbType.NVarChar).Value = pair.Value ?? DBNull.Value;
}
}
}
// check what type of query using the enums in the constants.cs file
if ((typeOfQuery == (typeOfQuery.insert)) || (typeOfQuery == typeOfQuery.update) ||
(typeOfQuery == typeOfQuery.delete))
{
return oCmd.ExecuteNonQuery();
}
else if (typeOfQuery == typeOfQuery.getDataSet)
{
SqlCeDataAdapter adapter = new SqlCeDataAdapter(oCmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
return dataSet;
}
else if (typeOfQuery == typeOfQuery.getDataTable)
{
SqlCeDataAdapter adapter = new SqlCeDataAdapter(oCmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
return dataSet.Tables[0];
}
else if (typeOfQuery == typeOfQuery.getScalar)
{
object returnValue = oCmd.ExecuteScalar();
if (returnValue == null)
{
return string.Empty;
}
else
return returnValue;
}
}
}
}
catch (SqlCeException ex)
{
throw;
}
catch (Exception ex)
{
throw;
}
finally
{
}
return false;
}
}
You can call this class as follows
string query = #"SELECT * FROM TABLE
WHERE COL1 = #COL1";
Dictionary<string, object> dictionaryToInsert = new Dictionary<string, object>();
dictionaryToInsert.Add("#COL1", Col1Value);
return (DataTable)new DataBaseHelper().ExecuteDatabaseQuery(query,
dictionaryToInsert, DataBaseHelper.typeOfQuery.getDataTable);
Similarly you can query database for other purposes also. use the enum and change the query and you will get the result.

Resources