|
I also have no idea what help you need, and would agree: Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know.
And DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.
You want an answer? Describe your problem, remembering that we can't see your screen, access your HDD, or read your mind. Tell us what you have tried, what problems you met, and what help you need. And don't shout at us...it may work with your mother but in the real world it is unlikely to help you with anything...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
You have posted a similar question in Q& A section:
"auto generate serial number in c# "
Please don't Spam... Have some patience to get you answer. Also, Explain the problem you are facing. We have not developed telepathic abilities till date. Until then, you need to explain stuff..
|
|
|
|
|
Here you go: 19386461206399246257047 .
/ravi
|
|
|
|
|
I hope that's guaranteed to be random[^]!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Ask me again, and I'll guarantee I'll give you a different one. You have my guid on that.
/ravi
|
|
|
|
|
Hi,
I'm creating a C# dll which is going to be called from another c# application.
The dll task is to print documents. It will queue the requests & print the docs 1 after another.
I would like the c# dll, once it has printed a doc, to notify the calling application.
Then a message will be displayed on the user's screen.
I'm trying to achieve this using Action<T> Delegate but can't get it to work.
What's the correct usage of Action<T> Delegate in both the dll & main application in this example?
Thanks
|
|
|
|
|
Member 11713942 wrote: but can't get it to work. Unfortunately that does not give us a lot to go on. Without seeing your code and the details of what happens we cannot offer much by way of advice. You may find the documentation[^] or Exploring Action Delegate[^] helpful.
|
|
|
|
|
You do realize that an Action<T> is a full-featured multi-cast delegate ? Outside "consumers" of an object-instance which offers access-to/exposes the Action can subscribe to it using += or directly assign to it.
I like using a strategy where a Property "slot" for a Public Action<T> is declared in one class (in your case the dll), and then another class (the consumer of the first class/dll) injects a reference to a valid method/lambda/delegate that matches the signature:
public Action<string> PrintJobDone { set; get; }
private void PrintJobUpdate(bool jobdone, string currentjobname, int jobnumber, int jobslefttodo)
{
if (jobdone && PrintJobDone != null)
{
PrintJobDone(string.Format("job completed: {0} / job#: {1} / jobslefttodo: {2}", currentjobname, jobnumber, jobslefttodo));
}
} In a consuming Form or object with a reference to your 'dll:
private void Form1_Load(object sender, EventArgs e)
{
yourDllInstance.PrintJobDone = printJobCallBack;
}
private void printJobCallBack(string msg)
{
}
«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.
modified 31-May-15 8:13am.
|
|
|
|
|
Thank you very much. Very helpful.
|
|
|
|
|
public delegate void Action (string msg);
Define the below method inside the class in the same .dll
private void doAction(string msg)
{
}
//Do the below action from where you are calling i.e initialize the delegate action
private Action print;
print = new Action (doAction);
A a= new A();
a.printaction = print;
printDoc();
public Action printaction;
printDoc()
{
printaction(msg);
}
modified 1-Jun-15 2:13am.
|
|
|
|
|
Thanks very much. That's very helpful.
|
|
|
|
|
Can you explain to me this line in your proposed code:
A a= new A();
What's A ?
Thanks
|
|
|
|
|
You need to use Delegate and Events to do this cleanly. I was just researching this same subject yesterday and found the following tutorial and code example very helpful. Rather than regurgitate what he said, I just suggest you download the project files and examine them.
Events and Delegates Simplified[^]
The nice thing is there is a complete VS project that actually runs and isn't larded with lots of unrelated functionality.
|
|
|
|
|
Simple Question,
How do you use SqlDataSourceEnumerator.Instance.GetDataSources() with newer versions of SQL? is there an alternate command, google has turned up nothing.
Does not list any instances, 4 on the network, all 2012 - 2014
HELP!
Thanks
EDIT: Solved using the code from Locate SQL Server instances on the local network[^]
modified 31-May-15 23:21pm.
|
|
|
|
|
Also have tried SmoApplication.EnumAvailableSqlServers();
0 results
|
|
|
|
|
Use a newer version of SMO.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
|
Need to change datatype of a variable without changing its value .
Suppose i need to change a : string a = "a1" to byte[]
But the value of the byte[] should have the same value i.e "a1"
|
|
|
|
|
Um...you can't, not really.
The problem is that a string is made of an array of chars, each of which is a 16 bit value in UNICODE, while an array of bytes contains 8 bit values.
You can get a byte stream which holds the data from the string:
string s = ...
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s); Or you can get an ASCII version which is more likely to work with more external applications:
string s = ...
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);
But an array of bytes is not an array of chars, and thus not a string.
And you definitely can't treat an array of bytes as a string!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I'm having an issue with my C# code. I m trying to insert data into my database from a form, unfortunately I keep getting issues with Object reference not set to an instance of an object.
Here s the code:
aspx
<asp:GridView runat="server" ID="gvNEW"
AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE"
BorderStyle="None" BorderWidth="1px" CellPadding="4"
EnableModelValidation="True" ForeColor="Black" GridLines="Vertical" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("Name") %>' ID="txtName" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("Project") %>' ID="txtProject" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tower">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("Tower") %>' ID="txtTower" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FunctionalArea">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("FunctionalArea") %>' ID="txtFunctionalArea" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ResourceName">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("ResourceName") %>' ID="txtResourceName" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CATWResourceName">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("CATWResourceName") %>' ID="txtCATWResourceName" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Org">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("Org") %>' ID="txtOrg" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="IndicateifbillingFP">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("IndicateifbillingFP") %>' ID="txtIndicateifbillingFP" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="IndicateifbillingTM">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("IndicateifbillingTM") %>' ID="txtIndicateifbillingTM" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DefaultFTE">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("DefaultFTE") %>' ID="txtDefaultFTE" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Active">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("Active") %>' ID="txtActive" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PersonnelResourceType">
<ItemTemplate>
<asp:TextBox runat="server" text='<%#Eval("PersonnelResourceType") %>' ID="txtPersonnelResourceType" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<br />
C#
protected void InsertButton_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
foreach (GridViewRow row in gvNEW.Rows)
{
gvNEW.Columns.RemoveAt(0);
cmd.CommandText = "INSERT INTO StaffTracking (Project, Tower, Functional Area, ResourceName, CATWResourceName, Org, IndicateifBillingFP, IndicateifBillingTM, DefaultFTE, Active, PersonnelResourceType)" + " VALUES ( '" + ((TextBox)row.FindControl("txtProject")).Text + "', '" + ((TextBox)row.FindControl("txtTower")).Text + "', '" + ((TextBox)row.FindControl("txtFunctional Area")).Text + "', '" + ((TextBox)row.FindControl("txtResourceName")).Text + "', '" + ((TextBox)row.FindControl("txtCATWResourceName")).Text + "', '" + ((TextBox)row.FindControl("TxtOrg")).Text + "', '" + ((TextBox)row.FindControl("txtIndicateifBillingFP")).Text + "', '" +
((TextBox)row.FindControl("txtIndicateifBillingTM")).Text + "', '" + ((TextBox)row.FindControl("txtDefaultFTE")).Text + "', '" + ((TextBox)row.FindControl("txtActive")).Text + "', '" + ((TextBox)row.FindControl("txtPersonnelResourceType")).Text + "')";
int numRegs = cmd.ExecuteNonQuery();
}
conn.Close();
ResetInputForm();
} :sigh:
|
|
|
|
|
Your code is vulnerable to SQL Injection[^].
NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
protected void InsertButton_Click(object sender, EventArgs e)
{
const string Query = "INSERT INTO StaffTracking (Project, Tower, [Functional Area], ResourceName, CATWResourceName, Org, IndicateifBillingFP, IndicateifBillingTM, DefaultFTE, Active, PersonnelResourceType) VALUES (@Project, @Tower, @FunctionalArea, @ResourceName, @CATWResourceName, @Org, @IndicateifBillingFP, @IndicateifBillingTM, @DefaultFTE, @Active, @PersonnelResourceType)";
using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString))
using (SqlCommand command = new SqlCommand(Query, connection))
{
connection.Open();
foreach (GridViewRow row in gvNEW.Rows)
{
command.Parameters.AddWithValue("@Project", ((TextBox)row.FindControl("txtProject")).Text);
command.Parameters.AddWithValue("@Tower", ((TextBox)row.FindControl("txtTower")).Text);
command.Parameters.AddWithValue("@FunctionalArea", ((TextBox)row.FindControl("txtFunctional Area")).Text);
command.Parameters.AddWithValue("@ResourceName", ((TextBox)row.FindControl("txtResourceName")).Text);
command.Parameters.AddWithValue("@CATWResourceName", ((TextBox)row.FindControl("txtCATWResourceName")).Text);
command.Parameters.AddWithValue("@Org", ((TextBox)row.FindControl("TxtOrg")).Text);
command.Parameters.AddWithValue("@IndicateifBillingFP", ((TextBox)row.FindControl("txtIndicateifBillingFP")).Text);
command.Parameters.AddWithValue("@IndicateifBillingTM", ((TextBox)row.FindControl("txtIndicateifBillingTM")).Text);
command.Parameters.AddWithValue("@DefaultFTE", ((TextBox)row.FindControl("txtDefaultFTE")).Text);
command.Parameters.AddWithValue("@Active", ((TextBox)row.FindControl("txtActive")).Text);
command.Parameters.AddWithValue("@PersonnelResourceType", ((TextBox)row.FindControl("txtPersonnelResourceType")).Text);
command.ExecuteNonQuery();
command.Parameters.Clear();
}
}
ResetInputForm();
}
The NullReferenceException is most likely due to the fact that your code is removing the column at index 0 on each pass through the loop.
- On the first row, this will remove the
txtName control, which you don't use. - On the second row, it will remove the
txtProject control. As a result, row.FindControl("txtProject") will return null , and you'll get a NullReferenceException when you try to access its Text property.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hey Norris,
please, for the love of cheese, first change your INSERT-statement to use Sql-Parameters, like I suggested you to do categorically in response to your first(?) post here. Simple example here: http://www.dotnetperls.com/sqlparameter[^]
- it prevents SQL-injection
- it makes your code more readable - for you and us
- it can prevent certain errors or make them easier to find
After you've done that there's a good chance the exception message will point to to the exact line so you'll see for yourself where the error is - otherwise please come back and post the reworked code and indicate the line at which the exception occurs.
You could also change it to instantiating the connection locally and using the connection and command object in using-blocks.
cheers, Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
That worked! Thanks Richard and Sascha. I will parameterized all of my code going forward.
I did get an error for this field: command.Parameters.AddWithValue("@DefaultFTE", ((TextBox)row.FindControl("txtDefaultFTE")).Text);
Because this field is decimal(8,3) and not nvarchar. Not a problem I was able to handle it.
One final question to all? Is there a way to fire off one method which in turn fire off another one? I'm doing an import and submit. I want the import to do the submit. O Can I include the SubmitButton_click code in the ImportButton_click code?
modified 29-May-15 16:10pm.
|
|
|
|
|
Norris Chappell wrote: Is there a way to fire off one method which in turn fire off another one? I'm doing an import and submit. I want the import to do the submit. O Can I include the SubmitButton_click code in the ImportButton_click code? Whenever you realize a requirement like this (needing the code that currently is "dedicated" to a certain place/event (SubmitButton_click) also in other situations) you should factor that code out into a separate method that can be used universally and gets all required inputs via arguments. Then you can call it from ImportButton_click and SubmitButton_click. No duplicate code and no weird codepaths (like calling SubmitButton_click from ImportButton_click).
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Sascha,
Thanks again! I was able to figure my last question out.
Regards,
Norris
|
|
|
|
|