method parameters error
-
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();
while (dr.Read())
{
return dr["ZoneCode"].ToString();
}
con1.Close();Error:
Compiler Error Message: CS0161: 'Adv.ReturnString(string, string, string, string, string, string)': not all code paths return a value
Line 51: public string ReturnString(string tbl, string colu, string cond1, string value1, string cond2, string value2)
pRemove the return from inside the while loop and place it outside. Return a collection of ZoneCodes instead of doing what you have done above.
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 objList = new List();
while (dr.Read())
{
objList.Add(dr["ZoneCode"].ToString(););
}
return objList;
con1.Close();You method signature will be
public List ReturnString(string tbl, string colu, string cond1, string value1, string cond2, string value2)
Sorry. I cannot include this code in the
tags. The generics part is getting messed up.
-
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.
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 objList = new List();
while (dr.Read())
{
objList.Add(dr["ZoneCode"].ToString(););
}
return objList;
con1.Close();You method signature will be
public List ReturnString(string tbl, string colu, string cond1, string value1, string cond2, string value2)
Sorry. I cannot include this code in the
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?)
-
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
-
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.
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 objList = new List();
while (dr.Read())
{
objList.Add(dr["ZoneCode"].ToString(););
}
return objList;
con1.Close();You method signature will be
public List ReturnString(string tbl, string colu, string cond1, string value1, string cond2, string value2)
Sorry. I cannot include this code in the
tags. The generics part is getting messed up.
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.
-
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.
-
No problems. It's the joy of coding directly in the html textbox. :-D
"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.
-
No problems. It's the joy of coding directly in the html textbox. :-D
"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.
-
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.
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 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]"
-
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 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.
-
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.
how? give me code in c#
-
how? give me code in c#
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.
-
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.
I got a null value. please help me to return a string and not
List
I'd like to change to the previous one... :)
-
I got a null value. please help me to return a string and not
List
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.
-
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.
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();
//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;
}
}
} -
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();
//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;
}
}
}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.