|
Thanks, Alan !
I will experiment with your suggestion, and study the results today.
As I wrote to Sascha (here, today): "My goal here is that these Properties are initialized by a Static Class, not by any code in the Forms in which the custom Component(s) are placed/instantiated. Since, in a way, control of the instances of these custom Components, no matter what "container" they are in, is a kind of "cross-cutting concern" (to mis-use a term from the AOP religion), I do think using an ApplicationContext here may be the best way to go, and it's an alternative WinForm architecture (better to say it's the "real" WinForm architecture behind-the-scenes whether or not the programmer ever realizes that) I am very familiar with."
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut, Jr.
|
|
|
|
|
I made a add-in to Word. At run time i need to read a XML file. This file is in the folder where the DLL was installed.
When I used to get the path "Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)", it returns "C:\Users\aguedes\AppData\Local\assembly\dl3\DMVXVA87.YEZ\G1QWDL4J.EQ8\ae9e683c\4479034f_9994d001"
How i get the correct path?
thanks a lot
|
|
|
|
|
Instead of the "executing" assembly, get the path to the current assembly.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thank you Eddy.
I resolve my problem by use:
System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
I saw this in http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in
|
|
|
|
|
Thank you for posting the solution.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I am using a console application, I want to implement 'Save As' functionality in it just like we do in any word file etc, in windows application we have 'SaveFileDialog' control but console application do not have this control, I am not getting any way out to implement it. So, please help me in implementing it. File.Copy(source, target) will not serve my purpose so, please avoid it.
Thanks in advance.
|
|
|
|
|
Considering there are no controls at all in a Console application, how do you expect to the user to specify they want to do a "Save As..."?
|
|
|
|
|
Save As path will be hard coded or any timeStamp.
|
|
|
|
|
Customarily, "Save As..." lets the user pick the location and the filename they want the data saved to. They also get the choice of what supported file format they want the data saved to.
So, you're going to have to ask the user for the filepath and in which format they want to save in.
Your also going to have to write code to save data in whatever formats you want to support.
Beyond that, we have no idea what part of this process you're having a problem with.
|
|
|
|
|
Since it is a console-application, I'd recommend to "Console.ReadLine" the path to where the user wants to save his stuff.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
What kind of abusing words are you using here Eddy and from whom are you using it, if you are not interested in answering do not write anything.
|
|
|
|
|
Eddy did answer you. And what you are probably referring to as "abusing words" is his signature, which, if you know what a signature is, is obviously not directed at you or anyone else in particular. And it's just a play on http://en.wikipedia.org/wiki/Bastard_Operator_From_Hell[^]
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Ok, now I understood, its a signature of Eddy. Ok, leave all this things, please pay attention on my query, please help me if you can.
|
|
|
|
|
I can't give you any more advice than you were already given: If you want a "Save As"-Dialog then you have to switch to a Windows Forms- or WPF-project. Else your only option is to read user-input via Console.ReadLine(..).
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
If you really *must* have a console application and you really *must* have a SaveFileDialog then you could embed the form into a DLL and call that.
There is an example here[^] but please heed the other advice already offered
|
|
|
|
|
hello again guys thanks for ur useful answer today i'm going to ask about this message that i get stuck with here is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Default3 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Data"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadUsersData();
}
}
protected void loadUsersData()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from UsersData", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
int count = ds.Tables[0].Rows.Count;
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gridView.DataSource = ds;
gridView.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gridView.DataSource = ds;
gridView.DataBind();
int columncount = gridView.Rows[0].Cells.Count;
lblmsg.Text = " No data found !!!";
}
}
protected void gridView_RowEditing(object sender, GridViewEditEventArgs e)
{
gridView.EditIndex = e.NewEditIndex;
loadUsersData();
}
protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = gridView.DataKeys[e.RowIndex].Values["id"].ToString();
TextBox UserName = (TextBox)gridView.Rows[e.RowIndex].FindControl("txtUserName");
TextBox Password = (TextBox)gridView.Rows[e.RowIndex].FindControl("txtPassword");
con.Open();
SqlCommand cmd = new SqlCommand("update UsersData set UserName='" + UserName.Text + "', Password='" + Password.Text + "' where id=" + id, con);
cmd.ExecuteNonQuery();
con.Close();
lblmsg.BackColor = Color.Blue;
lblmsg.ForeColor = Color.White;
lblmsg.Text = id + " Updated successfully........ ";
gridView.EditIndex = -1;
loadUsersData();
}
protected void gridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gridView.EditIndex = -1;
loadUsersData();
}
protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = gridView.DataKeys[e.RowIndex].Values["id"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("delete from UsersData where id=" + id, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
loadUsersData();
lblmsg.BackColor = Color.Red;
lblmsg.ForeColor = Color.White;
lblmsg.Text = id + " Deleted successfully....... ";
}
}
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string id = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "id"));
Button lnkbtnresult = (Button)e.Row.FindControl("ButtonDelete");
if (lnkbtnresult != null)
{
lnkbtnresult.Attributes.Add("onclick", "javascript:return deleteConfirm('" + id + "')");
}
}
}
protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox inid = (TextBox)gridView.FooterRow.FindControl("inid");
TextBox inUserName = (TextBox)gridView.FooterRow.FindControl("inUserName");
TextBox inPassword = (TextBox)gridView.FooterRow.FindControl("inPassword");
con.Open();
SqlCommand cmd =
new SqlCommand(
"insert into UsersData(id,UserName,Password) values('" + inid.Text + "','" +
inUserName.Text + "','" + inPassword.Text + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
loadUsersData();
lblmsg.BackColor = Color.Green;
lblmsg.ForeColor = Color.White;
lblmsg.Text = inid.Text + " Added successfully...... ";
}
else
{
lblmsg.BackColor = Color.Red;
lblmsg.ForeColor = Color.White;
lblmsg.Text = inid.Text + " Error while adding row.....";
}
}
}
protected void gridView_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
thanks..
|
|
|
|
|
Do you want to us a hint where exactly the exception is being thrown?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
Don't post bad images to show your error: use copy and past so we can actually read it!
Guessing from the "shape" of the code it's in this line:
SqlCommand cmd =
new SqlCommand(
"insert into UsersData(id,UserName,Password) values('" + inid.Text + "','" +
inUserName.Text + "','" + inPassword.Text + "')", con);
Which mean you need to look at idid, inUSerName, and inPassword to see that they are all valid and not-null
But don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. This is particularly important in web based apps where anyone can delete your database from the other side of the world just by typing into your textboxes!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
It points to a small part of your code;
SqlCommand cmd =
new SqlCommand(
"insert into UsersData(id,UserName,Password) values('" + inid.Text + "','" +
inUserName.Text + "','" + inPassword.Text + "')", con);
It is most likely that "inUserName" or "inPassword" controls are not found; hence, the exception. You should look into SQL injection; some idiot like me might otherwise wipe your entire database, instead of simply logging in. Stop concatenating strings - it is a cardinal sin. I'd also recommend to place the command in a using, to automatically dispose it, and to use the factory-method on the connection to create a command of the type of connection you are using:
using(SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "insert into UsersData(id,UserName,Password) values(@id,@username,@password)";
cmd.Parameters.AddWithValue("id", 1);
cmd.Parameters.AddWithValue("username", "johndoe");
cmd.Parameters.AddWithValue("password", "No");
if (1 != cmd.ExecuteNonQuery)
System.Diagnostics.Debugger.Break();
}
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: if (1 != cmd.ExecuteNonQuery)
You're missing the parentheses on that method call:
if (1 != cmd.ExecuteNonQuery())
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That's what the compiler will say. Speaking of, when do we get automatic error-highlighting for compiler-warnings on code-snippets?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I am writing an application to transfer data from a simulated OPC server to a MySql Database. I am using some of the libraries from OPC Foundation - OPCNETAPI.DLL, OPCNETAPI.COM.DLL.
I have the program reading from the server happily, but it seems as soon as I open a connection inside the loop of reading data, it loops about 5 times and then stops executing.. There are no exceptions thrown and I have tried for hours to work it out and miserably failed.
I've also changed VisualStudio to break when an exception is thrown, and it doesn't
Hopefully some one may know whats going on, but there is nothing obvious, hopefully the following code will spark ideas!
**Windows Forms Code**
public void readplc()
{
Opc.URL url = new Opc.URL("opcda://localhost/Matrikon.OPC.Simulation.1");
Opc.Da.Server server = null;
OpcCom.Factory fact = new OpcCom.Factory();
server = new Opc.Da.Server(fact, null);
try
{
server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential()));
}
catch (Exception ecy)
{
}
Opc.Da.Subscription group;
Opc.Da.SubscriptionState groupState = new Opc.Da.SubscriptionState();
groupState.Name = "Group";
groupState.Active = true;
group = (Opc.Da.Subscription)server.CreateSubscription(groupState);
Opc.Da.Item[] items = new Opc.Da.Item[2];
items[0] = new Opc.Da.Item();
items[0].ItemName = "Random.Int1";
items[1] = new Opc.Da.Item();
items[1].ItemName = "Random.Time";
items = group.AddItems(items);
group.DataChanged += new Opc.Da.DataChangedEventHandler(OnTransactionCompleted);
}
public void OnTransactionCompleted(object group, object hReq, Opc.Da.ItemValueResult[] items)
{
for (int i = 0; i < items.GetLength(0); i++)
{
try
{
string a = items[i].Value.ToString();
string b = items[i].ItemName;
string c = items[i].Key;
MySqlConnection conn = new MySqlConnection("server=localhost;user id=localhost;password=localhost;database=localhost;pooling=false");
conn.Open();
conn.Close();
}
catch (Exception ec)
{
}
}
}
I have set break points throughout the code, stepped through everything possible and still nothing obvious that's causing the issue.
modified 22-May-15 9:17am.
|
|
|
|
|
Why do you open a connection and close it on the next line in OnTransactionCompleted(..) ?
Don't swallow exceptions: You catch exceptions in readplc() and OnTransactionCompleted(..) and then do nothing so you aren't aware of any exceptions being thrown.
I'd suggest: Remove/comment the code that opens and closes the connection. Put a MessageBox.Show(..) in the catch-blocks to show the exception message (just for simple debugging, that's not the right thing to do in production code).
If that doesn't already lead you to being able to fix the problem, please "report back" And please elaborate on what you mean by "stops executing": Does it exit the loop or does it "hang" (on which line?) or something different ?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I open and close the connection for the purpose of the program to keep looping without throwing an exception.
I can see if an exception is thrown when I am stepping through the code in debug mode.
Even with a messagebox.Show(ec.message); It never reaches the code.
It stops executing, it says its running but I know it's not. If I put a break point in between the loop when it seems to of 'crashed' the breakpoint is never reached..
I can't work out which line, but, I know if I remove the mysql lines of code, the program works nicely..
Probably worth noting that this code works perfectly in a console application, I have it coded to insert the data into a mysql database, and it doesn't crash at all.
modified 22-May-15 9:17am.
|
|
|
|
|