|
Remove the return from inside the while loop and place it outside.
Return a collection of ZoneCodes instead of doing what you have done above.
<pre>SqlConnection con1 = new SqlConnection();
con1.ConnectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ToString();
string sql1 = "select " + colu + " from " + tbl + " where " + cond1 + " ='" + value1 + "' and " + cond2 + " = " + value2 + "";
SqlCommand cmd1 = new SqlCommand(sql1, con1);
con1.Open();
SqlDataReader dr = cmd1.ExecuteReader();
List<string> objList = new List<string>();
while (dr.Read())
{
objList.Add(dr["ZoneCode"].ToString(););
}
return objList;
con1.Close();</pre>
You method signature will be
<pre>public List<string> ReturnString(string tbl, string colu, string cond1, string value1, string cond2, string value2)</pre>
Sorry. I cannot include this code in the <pre> tags. The generics part is getting messed up.
|
|
|
|
|
Compiler Error Message: CS0246: The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)
|
|
|
|
|
Abdul-Rhman Alsri wrote: Compiler Error Message: CS0246: The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)
You need to include the Systems.Collection.Generic namespace.
modified on Sunday, May 2, 2010 2:43 AM
|
|
|
|
|
A slight correction to this code moves the line con1.Close before the return - it's unreachable code, but you could always set ExecuteReader with the parameter CommandBehavior.CloseConnection to shut the connection automatically. I'd also suggest wrapping the code in using blocks to trigger disposal of the objects.
using (SqlConnection con1 = new SqlConnection())
{
con1.ConnectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ToString();
string sql1 = "select " + colu + " from " + tbl + " where " + cond1 + " ='" + value1 + "' and " + cond2 + " = " + value2 + "";
using (SqlCommand cmd1 = new SqlCommand(sql1, con1))
{
con1.Open();
using (SqlDataReader dr = cmd1.ExecuteReader())
{
List<string> objList = new List<string>();
while (dr.Read())
{
objList.Add(dr["ZoneCode"].ToString(););
}
return objList;
}
}
}
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Thanks - I completely missed the close() statement.
|
|
|
|
|
No problems. It's the joy of coding directly in the html textbox.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Pete O'Hanlon wrote: No problems. It's the joy of coding directly in the html textbox
I messed up the "dry run".
|
|
|
|
|
List<string> ZoneCode = ReturnZoneCode("select ZoneCode from AdZone where ID = 20 and AccountID=530");
TextBox1.Text = Convert.ToString(ZoneCode.ToString());
In the code above I called the function(Note: I edited the signature) and I want to bind the returned List<string> into a textbox.
Problem what I have that the Textbox is not bounded the the value returned from the call, but it had the following:
"System.Collections.Generic.List`1[System.String]"
|
|
|
|
|
That's because ToString returns this type of information on a list. If you want to display the values, you need to actually bind to an individual item.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
|
Well, suppose you wanted to display a list of all of the items in a textbox, you could use:
private void BindZones(List<string> zones)
{
StringBuilder sb = new StringBuilder();
foreach (string item in zones)
{
sb.AppendFormat("{0}{1}", item, Environment.NewLine);
}
TextBox1.Text = sb.ToString();
} To be honest though - you would be better off displaying this in a ListBox rather than a Textbox.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I got a null value.
please help me to return a string and not
List<string>
I'd like to change to the previous one...
|
|
|
|
|
Your code is missing - please show it.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I want the function to return a string and not a list string
Function:
public List<string> ReturnZoneCode(string sql1)
{
using (SqlConnection con1 = new SqlConnection())
{
con1.ConnectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ToString();
using (SqlCommand cmd1 = new SqlCommand(sql1, con1))
{
con1.Open();
using (SqlDataReader dr = cmd1.ExecuteReader())
{
List<string> objList = new List<string>();
while (dr.Read())
{
objList.Add(dr["ZoneCode"].ToString());
}
return objList;
}
}
}
|
|
|
|
|
Use a StringBuilder instead of a list.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Error in insert
string a = "'msman88'";
Needed to be inserted as 'msman88'
Error :
Incorrect syntax near 'msman88'.
Line 70: cmd_Account.ExecuteNonQuery();
|
|
|
|
|
in SQL single quote's can be used to delimit string literals, so if you need them as part of your data, try one of the following:
1. use SQLParameter, which is always the better option as it prevents SQL Injection vulnerability;
2. or double the single quote(s) inside your data
Now why would you want single quotes at the extremes of your data in the first place? Maybe you are misinterpreting some piece of documentation?
|
|
|
|
|
Abdul-Rhman Alsri wrote: string a = "'msman88'";
Try string a = @"''msman88''" .
modified on Saturday, May 1, 2010 11:22 PM
|
|
|
|
|
Hi
How can i bring my graphic shape to Front of all Controls in form.
my graphic is :
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen my2 = new Pen(Color.Black, 0.1f);
my2.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
e.Graphics.DrawRectangle(my2, 99, 52, 133, 99);
}
|
|
|
|
|
Don't think you can... You cannot set the z-index of controls like on a web form for example...
Why do you need your drawing in front of other controls?
|
|
|
|
|
AFAIK you can't - a form is drawn first then the controls are drawn on the form's surface.
The easiest way to acheive what you want is probably to create a DashedRectangle control that has a transparent background and make sure that's the last control to be drawn.
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hey,
Is there a way to use in a C# code the MS Dictionaries?
I want to take a word in a specific langugae and get the translation in other language...is it possible?
|
|
|
|
|
Maybe this will give you a start.
|
|
|
|
|
I am working on an application where I am using C# .net as well as ATL COM.
I have created below classes
1) Class Add, Which is created using C# .Net. And which implement Addition functionality. This class uses IAdd interface which is having Add method to perform addition. (Please note that IAdd is an com interface and we have used it by creating interop dll)
public class Add : IAdd
{
#region IAdd Members
void IAdd.Add(double P1, double P2)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
2) Class Calculate (com visible component), which is created using C# .Net. And which internally uses Class ADD to perform addition. . (Please note that ICustom is an com interface and we have used it by creating interop dll)
class Calculate : ICustom
{
#region ICustom Members
Add add = new Add ();
#endregion
#region ICustom Members
void ICustom.CustomQueryInterface(Guid id, ref object ptr)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
3) Class Client, This class is created using ATL COM, which crates the object of class Calculate as “calculate“ and then tries to retrieve interface IAdd using object calculate.
To get the IAdd interface, what I have done is I have created a Custom Interface which contain a method similar to the Query Interface () method available in the IUnknownInterface.
This interface is then implemented by the class Calculate, Now whenever client requires an IADD interface pointer; it will use ICustomInterface to retrieve it.
Please suggest:
What should be the implementation Of .CustomQueryInterface(Guid id, ref object ptr)?
Also what should be the code at client side (Client is implemented inCOM)
IDL file :
[
object,
uuid(E99CD31D-34FE-4AFF-A1E6-C438F73F685F),
dual,
nonextensible,
helpstring("ICOMClient Interface"),
pointer_default(unique)
]
interface ICOMClient : IDispatch{
[id(1), helpstring("method PerformCalculation")] HRESULT PerformCalculation(void);
};
[
object,
uuid(53F4B9FA-B6E7-42aa-939B-97B93010270F),
dual,
nonextensible,
helpstring("IAdd Interface"),
pointer_default(unique)
]
interface IAdd : IUnknown{
[id(1), helpstring("method Add")] HRESULT Add([in] DOUBLE P1, [in] DOUBLE P2);
};
[
object,
uuid(8C4DE0FA-6D4B-4c52-9E35-72D1908D3457),
dual,
nonextensible,
helpstring("ICustom Interface"),
pointer_default(unique)
]
interface ICustom : IUnknown{
[id(1), helpstring("method CustomQueryInterface")] HRESULT CustomQueryInterface(GUID id ,IUnknown** ptr);
};
|
|
|
|
|