473,809 Members | 2,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set TextBox MaxLength to match SQL varchar(nnn) length?

I have a database with text string fields defined by varchar(nnn). When I
request from the user the text from a textbox, I'd like to set the maximum
number of characters in the textbox to the "nnn" that was used in the
varchar(...) statement in the field definition. i.e.

Field1 varchar(25) NOT NULL

and in the .aspx file:

<asp.textbox id=Field1 runat="server" />

I load a dataset and put data in the text box via the following:

DataSet dsInfo = new DataSet();
SqlDataAdapter daRecord
= new SqlDataAdapter( "SELECT * FROM Records WHERE RecordID ='"
+ RecordID + "'", SqlConnect);
daRecord.Fill(d sInfo, "Table");

DataTable dtRecord = dsInfo.Tables["Table"];
DataRow drRecord = dtRecord.Rows[0];

textBox1.Text = drRecord["Field1"]
textBox1.MaxLen gth = ???

My question is how can I set the MaxLength property of the text box?
Somewhere in the depths of the DataSet must be some description of the
schema defining the field in the table. Could someone offer guidance as to
extracting this? Presumably, I should also be able to extract other details
of the schema such as type or nullability.

I could have an array of sizes for each one of the text boxes but it seems
bad programming practice to have the field sizes defined in two completely
different places.

Ed
--
Edward E.L. Mitchell
Phone: (239)415-7039
6707 Daniel Court
Fort Myers, FL 33908

Jul 21 '05 #1
5 7460
Hi Edward,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get the MaxLength of a
certain column from SQL database. If there is any misunderstandin g, please
feel free to let me know.

I think we can do the following to achieve this.

1. Before filling the DataSet, we first fill the schema.

daRecord.FillSc hema(ds, SchemaType.Sour ce);
daRecord.Fill(d sInfo, "Table");

2. Then we use the DataColumn.MaxL ength to get the maximum length. If the
column has no maximum length, the value is -1.

textBox1.Text = drRecord["Field1"]
int len = dsInfo.Tables["Table"].Columns["Field1"].MaxLength
if(len != -1)
textBox1.MaxLen gth = len;

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jul 21 '05 #2
It's the FillSchema(...) that was the routine that I was looking for so that
I could interrogate the table definition.

However, I seem to get only the -1 that says that it has no maximum length!

I define the test table with:

CREATE TABLE Records
(
RecordID int NOT NULL IDENTITY PRIMARY KEY,
Field1 varchar(15) NOT NULL
)
INSERT Records (Field1) VALUES ("abc")
GO

and in the page load event do the following:

private void Page_Load(objec t sender, System.EventArg s e)
{
// database connection string
String SqlConnect = "workstatio n id=DELL340;pack et size=4096;"
+ "integrated security=SSPI;d ata source=DELL340; "
+ "persist security info=False;init ial catalog=Test";

DataSet dsInfo = new DataSet();

// read all the stuff from the table
SqlDataAdapter daRecord
= new SqlDataAdapter( "SELECT * FROM Records", SqlConnect);
daRecord.FillSc hema(dsInfo, SchemaType.Sour ce);
daRecord.Fill(d sInfo, "Records");
DataTable dtRecord = dsInfo.Tables["Records"];
DataColumn dc1 = dtRecord.Column s["Field1"];
int iLen1 = dc1.MaxLength;
}

When I single step through this code, the iLen1 shows as -1 rather than the
15 that I had put into the varchar(...) field definition that was used when
the table was created.

I looked (with the Debugger) into the DataSet dsInfo and found that there
was a list of {System.Data.Da taColumns} of length 2 and the second column
had the right name Field1 and a length of 15.

When I look into the DataTable dtRecord (the QuickWatch window expression
is:
((System.Collec tions.ArrayList )(((System.Data .DataColumnColl ection)(dtRecor d.Columns)).Lis t))._items[1])
then the field name is still "Field1" but the MaxLength has been changed
to -1. I tried both SchemaType of Source or Mapped with the same results.

Why doesn't the 15 length carry over into the DataColumn?

Ed


"Kevin Yu [MSFT]" <v-****@online.mic rosoft.com> wrote in message
news:$B******** ******@cpmsftng xa10.phx.gbl...
Hi Edward,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get the MaxLength of a
certain column from SQL database. If there is any misunderstandin g, please
feel free to let me know.

I think we can do the following to achieve this.

1. Before filling the DataSet, we first fill the schema.

daRecord.FillSc hema(ds, SchemaType.Sour ce);
daRecord.Fill(d sInfo, "Table");

2. Then we use the DataColumn.MaxL ength to get the maximum length. If the
column has no maximum length, the value is -1.

textBox1.Text = drRecord["Field1"]
int len = dsInfo.Tables["Table"].Columns["Field1"].MaxLength
if(len != -1)
textBox1.MaxLen gth = len;

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jul 21 '05 #3
Hi Edward,

You are getting -1 as the MaxLength of the DataColumn because when you
fillschema, the DataAdapter created a table named "Table". But when you
call Fill, the data was filled to another table named Records. So actually
the schema information was not put to the Record table. Thus, we have to
add a table name for FillSchema as the third argument. Please replace
FillSchema with the following line, which will resolved this issue.

daRecord.FillSc hema(dsInfo, SchemaType.Sour ce, "Records");

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jul 21 '05 #4
Kevin,

That fix worked. I appreciate the help.

The documentation (MSDN Oct2004) for the .NET Framework Class Library
doesn't mention the availability of a third argument to FillSchema(...) ! It
does say that FillSchema adds a DataTable named "Table" to the specified
DataSet so I could have extracted the lengths from this.

Ed

"Kevin Yu [MSFT]" <v-****@online.mic rosoft.com> wrote in message
news:0g******** ******@cpmsftng xa10.phx.gbl...
Hi Edward,

You are getting -1 as the MaxLength of the DataColumn because when you
fillschema, the DataAdapter created a table named "Table". But when you
call Fill, the data was filled to another table named Records. So actually
the schema information was not put to the Record table. Thus, we have to
add a table name for FillSchema as the third argument. Please replace
FillSchema with the following line, which will resolved this issue.

daRecord.FillSc hema(dsInfo, SchemaType.Sour ce, "Records");

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jul 21 '05 #5
Hi Ed,

It was nice to hear that the problem is resolved.

Actually, you can find this overload for FillSchema from the following link.

http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemdata commondbdataada pterclassfillsc hematopic3.asp

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
375
by: Gil | last post by:
I am trying to advance to the next textbox when input for the current textbox has reached its maximum length. In a different newsgroup I found this code snipit, but I have not been able to use it. Private Sub Text1_Change() If Len(Me.Text1) = Me.Text1.MaxLength Then Me.Text2.SetFocus End If End Sub
0
680
by: Edward Mitchell | last post by:
I have a database with text string fields defined by varchar(nnn). When I request from the user the text from a textbox, I'd like to set the maximum number of characters in the textbox to the "nnn" that was used in the varchar(...) statement in the field definition. i.e. Field1 varchar(25) NOT NULL and in the .aspx file: <asp.textbox id=Field1 runat="server" />
3
3119
by: Agnes | last post by:
How Can I set the max length in the datagrid ? Thanks
3
1323
by: N! Xau | last post by:
Hi, I am using a textbox to display, row by row, results of Database-related operation. One, is this a valid method or not? Two, I see textbox.maxlength defaulted to 32767. What problems do I run against increasing this limit? Three: Information to be displayed can easily over pass the limit.
0
1086
by: Christian Enklaar | last post by:
We are using a table with a primary key of type varchar. If we try to find entries with select * from <table> where <table.key> = '<text>'; entries with a key length of more than 32 characters are not found. Entries with a shorter key are found. Using "Like" instead of "=" works for varchar keys with length > 32 as well. Does anybody know about this Problem ? (We use PostgresQL 7.4.1)
3
1774
by: Christian Enklaar | last post by:
Hello, we are using a table with a primary key of type varchar. If we try to find entries with select * from <table> where <table.key> = '<text>'; entries with a key length of more than 32 characters are not found. Entries with a shorter key are found. Using "Like" instead of "=" works for varchar keys with length > 32 as well. Does anybody know about this Problem ?
0
1117
by: ABC | last post by:
When set textbox's datasource and datamember to dataset, Is it auto set textbox maxlength properties with dataset's field length?
9
6559
by: The Confessor | last post by:
I declare the following variable in one of my structures... <VBFixedString(17)> Dim Name As String Fixed to 17 because it's written to a random-access file. Is there any way to tie the MaxLength of a TextBox to this value? Thanks, The Confessor
0
1173
by: Gilgamesh | last post by:
I'm retrieving a data column from SQL Server 2000 which has the type Varchar with length set to 10. The MaxLength method of DataColumn object returns -1 instead of the correct size which is 10. Does anybody have an idea why C# doesn't read the correct length? Here is the code foreach (DataColumn _dataColumn in importDataSet.Tables.Columns) {
1
2989
by: =?Utf-8?B?SC5CLg==?= | last post by:
Hi, How can I force TextBox to have more than 32767 in length. Max Length is not respected over 32767. I build a log window and need to display a lot of data. Thanks, Hugo
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.