Placing text from a database into a textbox in WPF - wpf

I'm working on a WPF application and i cant seem to get this right. I am brand new to WPF.
I want to get the text from the database into text boxes but i tried it with only one textbox the first time and i get this error:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: No value given for one or more required parameters.
This is the code i have so far, this is how it's done in winforms but i suppose not the same in WPF
dbconn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=ChampInfo1.accdb");
dbconn.Open();
string selectallSQL = "SELECT Passive " +
"FROM BChampInfo " +
"WHERE [Champ Name] = Aatrox";
dbcomm = new OleDbCommand(selectallSQL, dbconn);
OleDbDataReader dbread = dbcomm.ExecuteReader();
while (dbread.Read())
{
txtskillname1.Text = dbread["Passive"].ToString();
}
I don't know what is wrong though, any help will be appreciated.
All my oledb declaration are done at the top.

It can be the easy target for sql injection. The problem is in query. Try this
string selectallSQL = "SELECT Passive " +
"FROM BChampInfo " +
"WHERE [Champ Name] = ?";
dbcomm = new OleDbCommand(selectallSQL, dbconn);
dbcomm.Parameters.AddWithValue("#aat", "Aatrox");

Related

SSIS Custom Column Property

I have a general question I'm hoping someone can answer about creating custom properties for use in a Data Flow Component.
Is it possible to create custom properties for use at the column level? I can create custom properties at the component level, no problem, but that does me no good.
I want to add two properties (Encrypt and Decrypt) to the Input Column metadata.
Say I have a collection of Input columns col1, col2, col3. As a developer, I would like to set col3's Encrypt value to true so, at run time, col3 gets encrypted before being loaded into a database.
I have successfully encrypted and decrypted using a custom component. Still, I used the values "e" and "d" in the column Description and then evaluated that Description during PreExecute. I set a state object based on the value of Description and add it to a collection that is processed during ProcessInput. I don't think using the Description is a good thing to do, and that is the need for the custom properties.
Do SSIS columns have custom properties?
The answer is yes. SSIS columns are objects that inherit the IDTSColumn130 interface. As mentioned in the SSIS documentation, this interface contains a property called CustomPropertyCollection that contains the collection of IDTSCustomProperty100 objects added to the input by the component.
Some components add some custom property to the SSIS columns such as the Derived Column Transformation. As I know, a custom component called FriendlyExpression is used to store the expression in plain text form. But, there is no way to add custom properties in the Integration Services package designer (Visual Studio).
How to add Custom Properties?
I think the only way is to create packages programmatically and edit those values or develop a custom SSIS component that adds these properties at runtime.
This is an example of reading the custom properties of a Derived Column transformation using C#. (Reference)
foreach (IDTSInputColumn localIColumn in localInput.InputColumnCollection)
{
if (localIColumn.CustomPropertyCollection.Count == 2)
{
repository.AddAttribute(componentRepositoryID, localInput.Name + " [" + localIColumn.Name + "] [ID: " + localIColumn.ID.ToString() + "]", "From [" + localIColumn.UpstreamComponentName + "] " + FormatColumnDescription(localIColumn.Name, localIColumn.DataType, localIColumn.Length, localIColumn.Precision, localIColumn.Scale) + " Expression " +
localIColumn.CustomPropertyCollection["FriendlyExpression"].Value != null ? localIColumn.CustomPropertyCollection["FriendlyExpression"].Value.ToString() : "Not Available"
);
}
else
{
repository.AddAttribute(componentRepositoryID, localInput.Name + " [" + localIColumn.Name + "] [ID: " + localIColumn.ID.ToString() + "]", "From [" + localIColumn.UpstreamComponentName + "] " + FormatColumnDescription(localIColumn.Name, localIColumn.DataType, localIColumn.Length, localIColumn.Precision, localIColumn.Scale) + " Expression (See Ouput Column)");
}
//repository.AddObject(localIColumn.Name, "", ColumnEnumerator.ObjectTypes.Column, componentRepositoryID);
}
Alternatives
You can store the columns metadata within an external data source (SQL, XML, ...) and load it at runtime. Or you can use the Description property as you mentioned in your question.

Concurrency Error WinForms Binding Source Navigator

I have a form with customer info that needs to be processed one transaction per page. I'm using the binding navigator to manage my pagination.
It works in all but some cases. In the cases where it doesn't work, I have to open a different window to look up information and return it to the main form. Here is the code for that:
// save current work
updateDataTable();
// Open a window and get new customer info
// CurrentCustomer is returned from the opened window
using (SqlConnection cx = new SqlConnection(GetConnectionString()))
{
DataRowView dataRow = (DataRowView)procBindingSource.Current;
dataRow.BeginEdit();
dataRow["CUSTOMER"] = CurrentCustomer;
dataRow.EndEdit();
updateDataItems();
SqlCommand cmd = new SqlCommand(
#" select acct_no from cust_processing where id = #id ", cx);
cmd.Parameters.AddWithValue("#id", (int)dataRow["ID"]);
cx.Open();
var results = cmd.ExecuteScalar();
if (results != null)
{
dataRow.BeginEdit();
dataRow["ACCT_NO"] = results.ToString();
dataRow.EndEdit();
updateDataItems(); <------ CONCURRENCY ERROR
}
}
The error I am getting is a concurrency error. I think that I have more than one version of the row possibly ? I thought I was making sure that I was on the most recent version of the row by calling updateDataTable(). I am the only user so I know I am creating the problem myself.
Here is my update method which is called when I change pages or save and exit or want to write the commit the data:
void updateDataItems()
{
this.procBindingSource.EndEdit();
this.procTableAdapter.Update(xyzDataSet);
xyzDataSet.AcceptChanges();
}
I have tried executing updateDataItems from various places such as after I assign dataRow["ACCT_NO"] = results.ToString() or before and after assigning that.
I'm pretty much down to guess and check so any thoughts, help and advice will be appreciated and +1.
Okay -- so the problem was that I was trying to update the current row from the program and also using the binding navigator. They were not working together properly.
The solution was to add a text box to the form in the forms designer and set visible = false and bind it to ACCT_NO. Once I got the results from my other form, I just needed to set the .text property of the ACCT_NO textbox to the new value and the binding navigator managed all my updates for me correctly.
txtAcct_No.text = results.ToString();

Why isn't IronPython setting my Control.Text correctly?

I've been messing around with Python and IronPython this week and I've stumbled across an extremely annoying oddity.
Basically I am connecting to a MSSQL database and upon success setting self.label1.Text = "Connected to " + sqlConn.Database + " on " + sqlConn.DataSource, however my label is updating to just say "Connected to" on the form. I placed a MessageBox.Show(self.label1.Text) after and this displays all the correct information ("Connected to DATABASE on DATASOURCE"). My question is, why isn't IronPython setting my label text correctly on the form?
class MyForm(Form):
def __init__(self):
self.button1 = Button()
self.button1.Text = "Click Me!"
self.button1.Click += self.button1_Click
self.Controls.Add(self.button1)
self.label1 = Label()
self.label1.Location = Point(10, 50)
self.Controls.Add(self.label1)
def button1_Click(self, sender, args):
sqlConn = self.connectSql()
if sqlConn.State == ConnectionState.Open:
self.label1.Text = "Connected to " + sqlConn.Database + " on " + sqlConn.DataSource
MessageBox.Show(self.label1.Text)
else:
self.label1.Text = "Failed connection"
def connectSql(self):
sqlConn = SqlClient.SqlConnection("Data Source=(local);Initial Catalog=IT_Project;Integrated Security=True;")
try:
sqlConn.Open()
except System.Exception as ex:
MessageBox.Show("Error!\r\n" + ex.Message, "EXCEPTION HANDLED")
return sqlConn
Thanks in advance. :)
The content of the Label is truncated because of its default size and behavior. By manually setting a sufficient size or using the AutoSize-Property the full content can be displayed.
The following code could be used after creating the label to size the label to its content:
self.label1.AutoSize = True
The fact that the message box displayed the full value from the actual control's text hinted to the underlying cause.
Note that according to the documentation the auto sizing default behavior may vary:
When added to a form using the designer, the default value is true. When instantiated from code, the default value is false.

Can I figure out a list of databases and the space used by SQL Server instances without writing SQL queries?

I need to get a list of SQL server instances present on a computer, get a list of databases in each instance, and then determine how much space each database is taking up.
I can easily grab the instance names from the registry, but I don't have access to query the tables to get the names of the databases. Is there another way of doing this, maybe though WMI?
After some digging around, I finally found the WMI Class that will get my the info I need. On a server where I have 3 instances of SQL Server, I found my data in the following classes
Win32_PerfFormattedData_MSSQLINST2_MSSQLINST2Databases
Win32_PerfFormattedData_MSSQLINST3_MSSQLINST3Databases
Win32_PerfFormattedData_MSSQLSERVER_SQLServerDatabases
My instances are MSSQLINST2, MSSQLINST3 and MSSQLSERVER. I couldn't figure out the naming scheme, so I had to look though all the classes to find out the information I needed. Anyway, here's the code that's working. Maybe someone will find it useful.
ManagementObjectSearcher sqlInstancesSearcher = new ManagementObjectSearcher(
new ManagementScope(#"Root\Microsoft\SqlServer\ComputerManagement10"),
new WqlObjectQuery("select * from SqlServiceAdvancedProperty where propertyindex = 12"),
null);
foreach (ManagementObject instance in sqlInstancesSearcher.Get())
{
string instanceName = instance["ServiceName"].ToString().Replace("$", String.Empty);
Console.WriteLine("INSTANCE: " + instanceName);
ManagementObjectSearcher classNameSearcher = new ManagementObjectSearcher(
new ManagementScope(#"root\cimv2"),
new WqlObjectQuery("select * from meta_class where __CLASS Like 'Win32_PerfFormattedData_" + instanceName + "%Databases%'"),
null);
foreach (ManagementClass wmiClass in classNameSearcher.Get())
{
string className = wmiClass["__CLASS"].ToString();
string query = "select * from " + className;
ManagementObjectSearcher databaseSearcher = new ManagementObjectSearcher(
new ManagementScope(#"root\cimv2"),
new WqlObjectQuery(query),
null);
foreach (ManagementObject database in databaseSearcher.Get())
{
Console.WriteLine(" " + database["Name"]);
Console.WriteLine(" Data Files : " + database["DataFilesSizeKB"]);
Console.WriteLine(" Log Files : " + database["LogFilesSizeKB"]);
Console.WriteLine(" Log Files Used : " + database["LogFilesSizeKB"]);
}
}
}

Filtering dataset with condition

I am using asp.net 2.0 and c#.
I have a dataset, which is getting the employee info. Now I want to filter the gridview based on a name that the user has put in the search textbox.
I am doing this:
DataSet ds = new DataSet("EmployeeInformation");
//........ loading DataSet ds with emploee info
string strExpr;
strExpr = "Name LIKE %" + txtSearchEmployee.Text.Trim() + "%";
ds.Tables[0].Select(strExpr);
I am getting an error in the last step, that the operator is missing.
Please guide me how can I achieve this. Thanks in advance.
You just need to add single quotes around your LIKE criteria:
strExpr = "Name LIKE '%" + txtSearchEmployee.Text.Trim() + "%'";
ds.Tables[0].Select(strExpr);

Resources