|
yes, there are alternatives for each of both steps. Having the two steps was the essence in my message.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.4
|
|
|
|
|
To be precise: My (1) and (2) were alternatives to your (2). They both assumes that your (1) was already done.
Greetings - Jacek
|
|
|
|
|
Of course; and that is how I understood it.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.4
|
|
|
|
|
Hi guys, i have a method like this: public List<t> listObj<t>() {}
and i want to instantiate a object with this type T, i tried this: T obj = new T(); doesnt work, so i tried T obj = default(T); and doesnt work too... how can i instantiete this obj??? Thanks.
The code:
public class MysqlDireto
{
private static MysqlDireto _instancia;
private static string connectionstring = "Server=;Database=;Uid=;Pwd=;";
public static MysqlDireto Instancia
{
get { return _instancia ?? (_instancia = new MysqlDireto()); }
}
public List<T> ObterLista<T>()
{
MySqlConnection conn = new MySqlConnection(connectionstring);
string comando = "SELECT * FROM " + typeof(T).Name;
MySqlCommand cmd = new MySqlCommand(comando, conn);
conn.Open();
MySqlDataReader leitor = cmd.ExecuteReader();
List<T> lista = new List<T>();
if (leitor.HasRows)
{
while (leitor.Read())
{
T obj = Foo<T>();
obj.GetType().GetProperty("Id").SetValue(obj, leitor["Id"], null);
lista.Add(obj);
}
}
conn.Close();
return lista;
}
public T Foo<T>() where T : class, new()
{
return new T();
}
}
modified on Wednesday, June 8, 2011 2:11 PM
|
|
|
|
|
you have to add new() in the where clause of the generic definition.
public T Foo<T>() where T : new(){
return new T();
}
Or such
|
|
|
|
|
i got this msg: Error 800 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method
|
|
|
|
|
I think the error message is clear about what you need to fix: pass your generic class a type that can be instantiated with new T() (i.e. a non-abstract, with a parameterless constructor).
|
|
|
|
|
but the T class is a non-abstract and has no constructor parameter
|
|
|
|
|
The class that you pass as the generic parameter for T must be like the 'Test' class in the example that Ennis posted below.
|
|
|
|
|
public class Test {
public Test() {
}
}
public class Bar {
public static void Test<T>() where T : Test, new() {
T t = new T();
}
}
...
Bar.Test <Test>();
|
|
|
|
|
Thanks a lot, it was missing "Test,"
|
|
|
|
|
Hi,
I have a couple of headings that need set to bold and underlined but having trouble getting this to work in itextsharp. Writing out to a pdf. I can get the heading set to bold or i can get it underlined but not both at the same time.
Font normalBold = FontFactory.GetFont("times", 10, Font.BOLD);
Font normalBoldUnderlined = FontFactory.GetFont("times",10,Font.UNDERLINE);
I have also tried to create a paragraph but include the heading and set it to underlined but for some reason it sets the whole document underlined..
Paragraph paragraphThree = new Paragraph("Exchanger Represents:\n", normalBold);
paragraphThree.SetAlignment("Justify");
paragraphThree.IndentationLeft = 20;
paragraphThree.Font.SetStyle(iTextSharp.text.Font.UNDERLINE);
Anyone any ideas apart from drawing a line as to how to do this?
Thanks in advance
Paul
|
|
|
|
|
Are those flags combinable (i.e. Font.BOLD | Font.UNDERLINE)?
|
|
|
|
|
Excellent BobJanova. That works perfectly.
Thanks for your answer
Paul
|
|
|
|
|
Welcome. This is a common pattern in .Net for combinations of options, for example control styles, anchoring, keyboard modifiers and as you found out font options.
|
|
|
|
|
I'll keep that in mind. Thought i was going nuts as I couldn't find an example anywhere on the net and I knew it would be something simple
Thanks again
|
|
|
|
|
A lot of hints to be found here[^].
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
modified on Friday, June 10, 2011 8:40 PM
|
|
|
|
|
|
Below there is code for update button the error for this code is mentioned in subject line.
private void update_Click(object sender, EventArgs e)
{
try
{
con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (securityname.SelectedItem.ToString() == dr[0].ToString())
{
break;
}
}
cmd.Parameters.AddWithValue("@User_Id", OleDbType.VarChar);
cmd.Parameters.AddWithValue("@Security_Name", OleDbType.VarChar);
cmd.Parameters.AddWithValue("@qty_bought", OleDbType.Integer);
cmd.Parameters.AddWithValue("@qty_sold", OleDbType.Integer);
cmd.Parameters.AddWithValue("@Rate_Security", OleDbType.Double);
cmd.Parameters.AddWithValue("@Total_security", OleDbType.Double);
cmd.Parameters.AddWithValue("@Brokerage", OleDbType.Double);
cmd.Parameters.AddWithValue("@Net_Rate", OleDbType.Double);
cmd.Parameters.AddWithValue("@Service_Tax", OleDbType.Double);
cmd.Parameters.AddWithValue("@Stt_Amt", OleDbType.Double);
cmd.Parameters.AddWithValue("@Total_Amt", OleDbType.Double);
cmd.Parameters.AddWithValue("@Net_Delivery", OleDbType.Double);
cmd.Parameters.AddWithValue("@Tran_Charges", OleDbType.Double);
cmd.Parameters.AddWithValue("@Sec_Tran_Charges", OleDbType.Double);
cmd.Parameters.AddWithValue("@Stamp_Duty", OleDbType.Double);
cmd.Parameters.AddWithValue("@Total_due", OleDbType.Double);
cmd.Parameters.AddWithValue("@NSE", OleDbType.Boolean);
cmd.CommandText = "Update Security SET qty_bought=@qty_bought,qty_sold=@qty_sold,Rate_Security=@Rate_Security,Total_Security=@Total_Security,Brokerage=@Brokerage,Net_Rate=@Net_Rate,Service_Tax=@Service_Tax,Stt_Amt=@Stt_Amt,Total_Amt=@Total_Amt,Net_Delivery=@Net_Delivery,Tran_Charges=@Tran_Charges,Sec_Tran_Charges=@Sec_Tran_Charges,Stamp_Duty=@Stamp_Duty,Total_due=@Total_due,NSE=@NSE Where Security_Name=@Security_Name AND User_Id=@User_Id";
securityTableAdapter.Update(strUserId, Convert.ToInt32(Qty_biught.Text), Convert.ToInt32(qty_sold.Text), Convert.ToDouble(gross_rate.Text), Convert.ToDouble(tot.Text), Convert.ToDouble(broker.Text), Convert.ToDouble(Net_rate.Text), Convert.ToDouble(Serv_Tax.Text), Convert.ToDouble(stt_amt.Text), dTot, Convert.ToDouble (Net_Del.Text), Convert.ToDouble(Tran_chr.Text), Convert.ToDouble(Sec_tran_tax.Text), Convert.ToDouble(stamp_duty.Text), Convert.ToDouble(bal_amt.Text), NSE.Checked, dr[0].ToString(), dr[1].ToString(), Convert.ToInt32(dr[2].ToString()), Convert.ToInt32(dr[3].ToString()), Convert.ToDouble(dr[4].ToString()), Convert.ToDouble(dr[5].ToString()), Convert.ToDouble(dr[6].ToString()), Convert.ToDouble(dr[7].ToString()), Convert.ToDouble(dr[8].ToString()), Convert.ToDouble(dr[9].ToString()), Convert.ToDouble(dr[10].ToString()), Convert.ToDouble(dr[11].ToString()), Convert.ToDouble(dr[12].ToString()), Convert.ToDouble(dr[13].ToString()), Convert.ToDouble(dr[14].ToString()), Convert.ToDouble(dr[15].ToString()), Convert.ToBoolean(dr[16].ToString()));
dr.Close();
cmd.ExecuteNonQuery();
Security_name.Text = securityname.Text;
securityname.Visible = false;
Security_name.Visible = true;
con.Close(); }
catch (Exception ex)
{
con.Close();
MessageBox.Show(ex.ToString());
}
}
Bold code is having a exception and I made all initialization regarding Connection and Command in Load_Form().
modified on Thursday, June 9, 2011 12:31 AM
|
|
|
|
|
Hi,
I'm not familiar with that exception.
I'm having difficulties reading your code, as you did not use PRE tags. You could still edit and add them.
I did see a few strange things:
1.
there are two very similar lines cmd.ExecuteReader(); and cmd.ExecuteNonQuery();
2.
the declarations and initialisations of con and cmd are missing.
3.
The normal way to do database accesses would be:
- to have an open,use,close pattern for the connection, relying on connection pooling. A using statement is extremely useful here, as it would close the connection for you.
- to have a create, use, dispose pattern for the command, again a using statement works well.
4.
The DataReader also needs being disposed of; again using is useful.
So it would end up looking like this:
using (OleDbConnection con=new OleDbConnection(...)) {
using (OleDbCommand cmd=new OleDbCommand(...)) {
using (OleDbDataReader dr = cmd.ExecuteReader()) {
while (dr.Read()) {
...
}
}
}
using (OleDbCommand cmd=new OleDbCommand(...)) {
cmd.Parameters.Add...;
cmd.ExecuteNonQuery();
{
}
Now con, cmd, dr automatically get closed/disposed even when exceptions were thrown.
PS: I have no idea what "AutoSizeMode" is doing in your subject line.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
Hello.
I'm implementing WebDAV COPY method.
public void Copy(string userName, string password, string source, string destination)
{
CredentialCache MyCredentialCache = new CredentialCache();
MyCredentialCache.Add(new System.Uri(source), "Basic", new NetworkCredential(userName, password));
try
{
HttpWebRequest Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(source);
Request.Credentials = MyCredentialCache;
Request.Method = "COPY";
Request.Headers.Add("Destination", destination);
Request.Headers.Add("Overwrite", "F");
HttpWebResponse Response = (System.Net.HttpWebResponse)Request.GetResponse();
Response.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
The method works fine with non-unicode file names.
But, if I want to copy a file with korean letters in its name (http://myDAVserver.myCompany.com/dav/한글.txt) I get the following error:
Message: Specified value has invalid Control characters.
Parameter name: value
I tracked the error, and the exception is being throwed exactly in
Request.Headers.Add("Destination", destination);
Does anybody have an idea how to handle this problem?
Thank you.
|
|
|
|
|
The problem could be a conflict between the character sets allowed in URIs and filenames (on whatever system). I had a quick peek at the RFCs but the allowed character set for URIs didn't leap out at me. (The destination parameter is specified by the WebDAV RFC as being a URI.) Perhaps the documentation of HttpWebRequest specifies what character set it will allow in headers - it should, if it throws an exception when you get it wrong.
Not a solution on a plate, but hopefully this will point you in the right direction.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
HTTP headers have to be 7 bit ASCII, I think, nominally. They are certainly interpreted at best as 8 bit ANSI (Windows western encoding). You will need to encode the Unicode characters.
|
|
|
|
|
Thank you for the answer.
Yes. You are right.
I could have encoded the Unicode characters and URL using HttpUtility.UrlEncode Method.
But the problem is, it is implemented only in .Net 4.0, but unfortunately, I'm writing my program in .Net 3.5 do to the compatibility with "a little bit old OSs" with older versions of .Net.
Still did not find a proper solution.
That was totally wrong.
I tried to use HttpUtility.UrlPathEncode(str) method directly without adding reference to the System.Web Namespace in project's properties:
...
using System.Web;
...
string convertedPath=HttpUtility.UrlPathEncode(path);
...
And after adding reference, I did like this:
...
string convertedPath=System.Web.HttpUtility.UrlPathEncode(path);
...
and it worked.
Thank you guys!
|
|
|
|
|
I am trying to execute a delegate from Method B1 for ButtonC and I get the following error:
Object reference not set to an instance of an object.
Have I setup the delegate correctly, what am I doing wrong.
I appreciate any help.
Thanks in advance,
Michael
namespace Z
{
public delegate void DelC(object sender, EventArgs e);
static partial class A
{
public static void Main()
{
FormC FC = new FormC();
DelC delC = new DelC (FC.buttonC_Click);
}
}
public static class B
{
public static DelC delC = null;
public static void B1()
{
object sender = new object();
EventArgs e = new EventArgs();
delC(sender, e); *****ERROR - Object reference not set to an instance of an object.
}
}
public class FormC
{
public void buttonC_Click(object sender, EventArgs e)
{
SortData();
}
}
}
modified on Tuesday, June 7, 2011 10:11 PM
|
|
|
|
|