first of all thanks for taking your time, now
the problem i have is this;
I have different textblocks in my main XAML file, im trying to get their text to use them in a sqlquery from another file(.cs file)
public void FillTable()
{
MainWindow l = new MainWindow();
string sql = "insert into Pacientes (nombre) values ('"+ l.nombre_text.Text +"')";
var command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
l.Close();
}
However, when i check the Table the result is null, when i check the table the
"nombre" column is blank
See sql image
any clue what im doing wrong?
You can't do a "new MainWindow". You need to get a reference to your MainWindow or somehow pass in the text to FillTable().
Without seeing more of your code, an exact solution is not probable, but something along these lines might get you unblocked.
...
// in MainWindow.cs
FillTable(this);
...
public void FillTable(MainWindow window)
{
string sql = "insert into Pacientes (nombre) values ('"+ window.nombre_text.Text +"')";
var command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
OR
...
FillTable(nombre_text.Text);
...
public void FillTable(string nombre)
{
string sql = "insert into Pacientes (nombre) values ('"+ nombre +"')";
var command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
Related
help, combobox just keep adding items, i tried using removeallitems but after that i cant put anything on the first combobox
public class Function {
public void combofillsect(JComboBox section, String year){
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
String query;
try{
query = "Select Section from asd where Year=?";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
pst = conn.prepareStatement(query);
pst.setString(1, year);
rs = pst.executeQuery();
while(rs.next()){
section.addItem(rs.getString("Section"));
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
section.addItem(e.toString());
};
}
Function funct= new Function();
{funct.combofillsect(jComboBox1,String.valueOf(jComboBox2.getSelectedItem())); }
why cant I post image?
Are you programming in C# ? If it's the case then you can use the function Clear like that : yourComboBox.Items.Clear() to delete all the current items. I don't know if it will solve your problem but your technique of getting the data from your database seems weird to me, if you used a dataset you could have done dataset.Tables(0).Rows.Count() to get the number of entries and then set the exit condition of your loop like this -> counter < dataset.Tables(0).Rows.Count(), and set a counter++ at the end of your while (maybe that's why you say your combobox won't stop filling, but I don't know what do the next() function).
I don't know the C# code but there is my VB.NET function :
Public Function getAll() As DataSet
ConnectionDB()
Dim cmd As SqlClient.SqlCommand
cmd = New SqlClient.SqlCommand("SELECT * FROM table", Connect)//Connect is a System.Data.SqlClient.SqlConnection, or my connection string
Dim adapter As New Data.SqlClient.SqlDataAdapter
Dim dataset As New DataSet
adapter.SelectCommand = cmd
adapter.Fill(dataset)
adapter.Dispose()
cmd.Dispose()
Connect.Close()
Return dataset
End Function
I don't know if I helped you but I didn't really understood what your problem was and you didn't even mentioned the language you use ^^ Good luck
Edit : and if you can't post images, that's because you don't have yet 10 points of reputation, you can get informations about reputation here : https://stackoverflow.com/help/whats-reputation, but you still can post the link of a picture it will allow users to click on it
Consider the following code:
[Test]
public void StackOverflowQuestionTest()
{
const string connectionString = "enter your connection string if you wanna test this code";
byte[] result = null;
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (var sqlCommand = new SqlCommand("declare #xml as xml = '<xml/>' SELECT convert(varbinary(max), #xml) as value"))
//using (var sqlCommand = new SqlCommand("SELECT convert(varbinary(max), N'<xml/>') as value"))
{
sqlCommand.Connection = connection;
using (SqlDataReader reader = sqlCommand.ExecuteReader())
{
while (reader.Read())
{
result = (byte[])reader["value"];
}
reader.Close();
}
}
}
string decodedString = new UnicodeEncoding(false, true).GetString(result);
var document = XElement.Parse(decodedString);
}
If I run this test I get an XmlException with message : "Data at the root level is invalid. Line 1, position 1." As it turns out the problem is "0xFFFE" preamble which is considered as invalid character.
Note that if I use commented string instead, everything works just fine, which is strange as per me. Looks like SqlServer stores XML strings in UCS-2 with a BOM, and at the same time it stores nvarchar values without it.
The main question is: how can I decode this byte array to string which will not contain this preamble (BOM)?
In case anyone will need this in future, the following code works:
using(var ms = new MemoryStream(result))
{
using (var sr = new StreamReader(ms, Encoding.Unicode, true))
{
decodedString = sr.ReadToEnd();
}
}
I have a simple form with a textBox in it which I am trying to make an user friendly Auto-suggest Textbox..
As per the current scenario I am using AutoCompleteStringCollectionclass bywhich I am able to make textbox show suggestions for the word that starts with the particular Text entered in textbox..
But,I want to make my program as like it should show suggestions even when a part of the string coming from database matches the Textbox.Text.
Presently I am able to filter the data coming from DB based on the userInput by using dataView .But still I am not able to show output on my front end..
I have tried all textbox events like 'KeyPress',KeyDown,KeyUp,TextChanged but its not working....
MyCode::
public partial class Form2 : Form
{
AutoCompleteStringCollection autoCompletefromDB = new AutoCompleteStringCollection();
AutoCompleteStringCollection searchResults = new AutoCompleteStringCollection();
MyLinqDataContext dtcontext = new MyLinqDataContext();
// static string searchChar = "";
SqlConnection con = new SqlConnection("Data Source=DATASERVER\\SQL2K8;Initial Catalog=VTMMedicalContent;Persist Security Info=True;User ID=vtm;Password=M3d!c#l");
DataTable dTable = new DataTable();
SqlCommand cmd;
SqlDataAdapter da;
DataView dtView;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select DiagnosisName from [VTMMedicalContent].[dbo].[DiagnosisMaster]";
da = new SqlDataAdapter(cmd);
da.Fill(dTable);
dtView = new DataView(dTable);
}
//And My KeyPress Event Code..
private void txtAutoComplete_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsControl(e.KeyChar))
{
dtView.RowFilter = dtView.Table.Columns[0].ColumnName + " Like '%" + e.KeyChar + "%'";
foreach (DataRowView dtViewRow in dtView)
searchResults.Add(dtViewRow[0].ToString());
txtAutoComplete.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtAutoComplete.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtAutoComplete.AutoCompleteCustomSource = searchResults;
}
//MessageBox.Show("The Elements in searchResult are:" + searchResults.Count);
}
I have the tried writing the same codes in KeyDown,KeyUp,TextChanged events but of no use..:(
It only works on Form_Load but that only shows suggestions that matches the starting point of the word..
Create an AutoComplete TextBoxControl
The AutoComplete feature has a couple of quirks that were inherited from its original designed use, the address box of Internet Explorer. This includes emitting the Enter key when you click on an item in the list. Pressing Enter in the address box of IE makes it navigate to the entered URL.
There isn't anything you can do about that, the native interface (IAutoComplete2) has very few options to configure the way it works. It pokes the keystrokes into the text box by faking Windows messages. Which is one way you can tell the difference, the actual key won't be down. Something you can check by pinvoking GetKeyState(), like this:
as an example you can mess with this anyway you like
private void textBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.Enter && GetKeyState(Keys.Enter) < 0) {
Console.WriteLine("Really down");
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern short GetKeyState(Keys key);
In my package, I have an Execute Sql Task that sets the result set to a User variable. I then have a c# script task that needs to reference this User variable as a result set. I need the entire result set sent into my script tasks as the web service I am calling needs the entire result set in one shot.
This is the current code I am testing with. It isn't much as I am still trying to figure out where to go with it.
Any help with this is greatly appreciated
public void Main()
{
Variable resultSet = Dts.Variables["User::ZBatch_Order_Export_ResultSet"];
Dts.TaskResult = (int)ScriptResults.Success;
}
This is the update working code:
public void Main()
{
DataTable dt = new DataTable();
OleDbDataAdapter oleDa = new OleDbDataAdapter();
oleDa.Fill(dt, Dts.Variables["User::ZBatch_Order_Export_ResultSet"].Value);
foreach (DataRow row in dt.Rows)
{
Dts.Events
.FireError(0, "ZBatch - Script Task", row["orderDate"]
.ToString(), String.Empty, 0);
// Do some Webservice magic
}
Dts.TaskResult = (int)ScriptResults.Success;
}
So very close, to access the Value of a variable, you need to hit that property
public void Main()
{
Variable resultSet = Dts.Variables["User::ZBatch_Order_Export_ResultSet"].Value;
// do stuff here with resultSet and the webservice
Dts.TaskResult = (int)ScriptResults.Success;
}
Here I am trying to assign the datasource (using same code given in the sample application) and create a graph, only difference is i am doing it in WPF WindowsFormsHost. due to some reason the datasource is not being assigned properly and i am not able to see the series ("Series 1") being created. wired thing is that it is working in the Windows Forms application but not in the WPF one.
am i missing something and can somebody help me?
Thanks
<Window x:Class="SEDC.MDM.WinUI.WindowsFormsHostWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:CHR="clr- namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.Dat aVisualization"
Title="HostingWfInWpf" Height="230" Width="338">
<Grid x:Name="grid1">
</Grid>
</Window>
private void drawChartDataBinding()
{
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
string fileNameString = #"C:\Users\Shaik\MSChart\WinSamples\WinSamples\data\chartdata.mdb";
// initialize a connection string
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;
// define the database query
string mySelectQuery = "SELECT * FROM REPS;";
// create a database connection object using the connection string
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
// create a database command on the connection using query
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
Chart Chart1 = new Chart();
// set chart data source
Chart1.DataSource = myCommand;
// set series members names for the X and Y values
Chart1.Series"Series 1".XValueMember = "Name";
Chart1.Series"Series 1".YValueMembers = "Sales";
// data bind to the selected data source
Chart1.DataBind();
myCommand.Dispose();
myConnection.Close();
host.Child = Chart1;
this.grid1.Children.Add(host);
}
Shaik
With the following two changes, you can fix your code:
1 - Instead of
Chart1.Series"Series 1".XValueMember = "Name";
Chart1.Series"Series 1".YValueMembers = "Sales";
write
Chart1.Series["Series 1"].XValueMember = "Name";
Chart1.Series["Series 1"].YValueMembers = "Sales";
2 - Before the above code, insert the following lines:
Chart1.ChartAreas.Add("Default");
Chart1.Series.Add(new Series("Series 1"));