|
1. create table test(name varchar(20),status bit not null)
assume status has value 0 or 1. then fill to a dataset.
Bind a gridview directly to dataset.
Column status shows true or false on gridview which is ok.
To retrieve the status value from gridviewrow.cells[1].text return empty string.
Confirm.
I only read newbie introductory dummy books.
|
|
|
|
|
DataGridViews will return an empty string from a cell which has a null value (i.e. DBNull.Value). These cells will show as empty in the visible grid, too.
|
|
|
|
|
1. create table test(name varchar(20),status bit not null)
assume status has value 0 or 1. then fill to a dataset.
Bind a gridview directly to dataset.
Column status shows true or false on gridview which is ok.
To retrieve the status value from gridviewrow.cells[1].text return empty string.
Confirm.
I only read newbie introductory dummy books.
|
|
|
|
|
What is the SQL column type? If it's text (that seems to be what you are saying), the easiest way is
bool answer = "True" == (string)row[colname];
If it's an integer type, try
bool answer = (bool)(int)row[colname];
... assuming you use the standard 0 = false. (You might be able to cast to bool directly but I imagine you tried that before asking here.)
|
|
|
|
|
Thanks. The column is bit(0 or 1).
When i bind the column to gridview what i see from page is true or false.
When retrieve its value from the gridview row cell it shows blank string "" which cannot be converted to boolen value
I only read newbie introductory dummy books.
|
|
|
|
|
If the SQL type is bit or int then
bool b = (bool)reader["ColumnName"];
If the SQL type is text(varchar or what have you) then something like
bool b = (bool)Convert.ToInt32(reader["ColumnName"]); should work
All the best,
Dan
|
|
|
|
|
Thanks
I used a gridview row bound from a dataset. In sql type it is bit.
I used the dataset direct instead of gridview.
I only read newbie introductory dummy books.
|
|
|
|
|
Hello.
I took this small function from anouther project, and I slightly modified it to work with a string object.
It accepts a string, and it should add a '-' for every nth charicture, as set by the code.
private void encodeString(string input)
{
for (int i = 0; i < input.Length-1; i++)
{
if ((i + 1) % 6 == 0)
{
input.Insert(i, "-");
}
}
}
It simply does not alter the string.
I simply want to pass string...
string test = ("00000000000000000000");
and end up with...
string test = ("00000-00000-00000-00000");
Thank you for any help with this.
Kind Regards,
Stephen
|
|
|
|
|
Because Strings are immutable. That means once created, they cannot be changed.
You have to change this to either pass the string in by reference (you'll be returning a new string anyway) or change this so it returns a string. I prefer the latter...
|
|
|
|
|
Dave Kreskowiak wrote: or change this so it returns a string
Sorry to ask, but how would I do that from within my function?
Steve
|
|
|
|
|
Here's one way of doing it:
private string encodeString(string input)
{
StringBuilder buffer = new StringBuilder();
buffer.AppendFormat("{0}-{1}-{2}-{3}",
input.SubString(0, 5),
input.SubString(5, 5),
input.SubString(10, 5),
input.SubString(15, 5));
return buffer.ToString();
}
|
|
|
|
|
Thank you,
Worked perfect!
Regards,
Stephen
|
|
|
|
|
stephen.darling wrote: It simply does not alter the string.
strings are immutable, nothing you do in .NET will ever alter a string. At best you replace one string by another one, and that is exactly why string methods have a return value.
I suggest you choose an introductory book to C# and start studying it thoroughly, you seem to still be struggling with the basics of the language.
|
|
|
|
|
|
Luc wrote: nothing you do in .NET will ever alter a string
"you" wasn't referring to you!
And you really shouldn't.
|
|
|
|
|
"It's what I oughtn't to do, but I do anyway." -- Mel Brooks' History of the World Part 1
|
|
|
|
|
Very appropriately, it goes on: Will you convert? “No, no, no, no.” ref[^]
|
|
|
|
|
You're passing Mel Brooks by reference? 
|
|
|
|
|
of course, there's too much value to pass on.
|
|
|
|
|
private string encodeString(string input)
{
string output = string.Empty;
for (int i = 0; i < input.Length-1; i++)
{
if ((i + 1) % 6 == 0)
{
output = input.Insert(i, "-");
}
}
return output;
}
|
|
|
|
|
Hello.
I tried your code, however, it produces the following result...
"38459171915879268-601"
So, it appears to be adding the whole string and then a '-', instead of a '-' every 5 charictures?
Thank you,
Stephen
|
|
|
|
|
I apologize... I threw that in haste without really thinking it through... this is probably the easiest way to handle any length string...
static string encodeString(string input)
{
string output = string.Empty;
for (int i = 0; i < input.Length; i++)
{
output += input.Substring(i, 1);
if ((i+1) % 6 == 0 && i != (input.Length-1))
output += "-";
}
return output;
}
|
|
|
|
|
fcronin wrote: I apologize... I threw that in haste without really thinking it through... this is probably the easiest way to handle any length string...
No problem!
Thank you again,
Steve
|
|
|
|
|
And you can increment i by six if you like; you're not limited to incrementing by one.
|
|
|
|
|
stephen.darling wrote: It simply does not alter the string.
And it will not, no matter what you do. As others have already pointed out, strings are immutable in .NET which means they cannot be altered once they're created.
Take a look at this example:
string a = "abc";
a = a + "xyz";
You might be thinking that in line 2 the characters "xyz" is appended to the string a, but that is not the case. When line 2 is executed, the Runtime creates a new string that is the result of the concatenation i.e. "abcxyz" and makes the variable a point to the new string. The old "abc" string becomes garbage (since it has no valid reference) and is collected during the next GC cycle.
To make large changes to to strings, it is recommended to use the System.Text.StringBuilder class.
private string encodeString(string input)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < input.Length; i++) {
builder.Append(input[i]);
if ((i + 1) % 6 == 0) {
builder.Append("-");
}
}
return builder.ToString();
}
And use your method like this:
string input = "00000000000000000000";
string output = encodeString(input);
modified on Tuesday, August 16, 2011 2:45 PM
|
|
|
|