472,782 Members | 1,305 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

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(dsInfo, "Table");

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

textBox1.Text = drRecord["Field1"]
textBox1.MaxLength = ???

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 7325
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 misunderstanding, 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.FillSchema(ds, SchemaType.Source);
daRecord.Fill(dsInfo, "Table");

2. Then we use the DataColumn.MaxLength 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.MaxLength = 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(object sender, System.EventArgs e)
{
// database connection string
String SqlConnect = "workstation id=DELL340;packet size=4096;"
+ "integrated security=SSPI;data source=DELL340;"
+ "persist security info=False;initial catalog=Test";

DataSet dsInfo = new DataSet();

// read all the stuff from the table
SqlDataAdapter daRecord
= new SqlDataAdapter("SELECT * FROM Records", SqlConnect);
daRecord.FillSchema(dsInfo, SchemaType.Source);
daRecord.Fill(dsInfo, "Records");
DataTable dtRecord = dsInfo.Tables["Records"];
DataColumn dc1 = dtRecord.Columns["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.DataColumns} 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.Collections.ArrayList)(((System.Data.Data ColumnCollection)(dtRecord.Columns)).List))._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.microsoft.com> wrote in message
news:$B**************@cpmsftngxa10.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 misunderstanding, 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.FillSchema(ds, SchemaType.Source);
daRecord.Fill(dsInfo, "Table");

2. Then we use the DataColumn.MaxLength 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.MaxLength = 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.FillSchema(dsInfo, SchemaType.Source, "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.microsoft.com> wrote in message
news:0g**************@cpmsftngxa10.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.FillSchema(dsInfo, SchemaType.Source, "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/
frlrfsystemdatacommondbdataadapterclassfillschemat opic3.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
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....
0
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...
3
by: Agnes | last post by:
How Can I set the max length in the datagrid ? Thanks
3
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...
0
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...
3
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...
0
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
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...
0
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....
1
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
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.