|
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
|
|
|
|
|
You're welcome!
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Hi,
I have created a DLL class and added two UserControls in it, UC01 and UC02.
In my WinForm application, I want to reference to the DLL using:
Assembly objAsm = Assembly.LoadFrom(@"C:\UC_FILES\MyDLL.dll");
Now, how can I locate a specific user control from that DLL and add it using:
userControl.Controls.Add(selected_uc);
Thanks,
Jassim[^]
Technology News @ www.JassimRahma.com
|
|
|
|
|
By "Reflection". Use the GetTypes method of the assembly, and then iterate thru the returned array till you found your type. Then Invoke its constructor (GetConstructor ) to get an instance which you cast to the desired type.
|
|
|
|
|
Im working on video streaming using mvc3 c# ,html5 and jquery and im sorry i cant post that code,
Now i want to update that to support any kind of video format and im ready to rewrite the entire code if required. can you guys tell me which open source plugin supports nearly any video format (included jquery/javascript video plugin and c# streaming code) and thanks in advance
modified 29-May-15 6:34am.
|
|
|
|
|
Message Closed
modified 1-Jun-15 8:55am.
|
|
|
|
|
way too much code man. You can add a small snippet, but nobody will read this much code.
What is the exact problem and what did you already try to solve that problem?
|
|
|
|
|
I'm not going to wade through that lot trying to work out what might be missing and what might not - it's not a good use of my time.
So help us to help you: edit your question, delete the code, and explain in English what you need help with. Then add small code fragments (enclosed in <pre> tags via the code widget to preserve the formatting) where they help your description.
But just dumping your code on us is rude, unhelpful, and very unlikely to get you the result you want.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
How to add Microsoft.Sharepoint.WebControls in C# Windows Application??
|
|
|
|
|
You can add a reference to the assembly like any other, but you can't reuse WebControls in a WinForms application.
..well, theoretically you could show them in a browser-control; but that would require a webserver, and those controls to be created on the server, shown in the browser - would make communication rather hard.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|