467,858 Members | 1,260 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,858 developers. It's quick & easy.

Reading an SQL database

Des
I am new to .NET so this code will not be the best. I need to read
fields from a single record and place them into individual variables.
What I have is this and the error is

BC30311: Value of type 'System.Data.DataColumn' cannot be converted to
'String'.

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<%
ID = request("ProdID")

Dim strConn as String = ConfigurationSettings.AppSettings("Thermos")
Dim objConn as new SqlConnection(strConn)

dim strItem as String = "SELECT * FROM Products WHERE ProductID = " &
ID

dim Adapter as new SqlDataAdapter(strItem,objConn)
dim DataSet as new DataSet()
Adapter.Fill(DataSet, "ITEM")

Dim Description as String
Description = DataSet.Tables("ITEM").Columns("Description")
%>
<html> ..... </html>

Any help on this one please

Desmond.

Feb 22 '06 #1
  • viewed: 1281
Share:
7 Replies
Hi,

I believe ID is an integer value

change the statement from
dim strItem as String = "SELECT * FROM Products WHERE ProductID = " &
ID.
to
dim strItem as String = "SELECT * FROM Products WHERE ProductID = " &
ID.ToString()

This should work..

Vinu

"Des" <de*********@aol.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I am new to .NET so this code will not be the best. I need to read
fields from a single record and place them into individual variables.
What I have is this and the error is

BC30311: Value of type 'System.Data.DataColumn' cannot be converted to
'String'.

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<%
ID = request("ProdID")

Dim strConn as String = ConfigurationSettings.AppSettings("Thermos")
Dim objConn as new SqlConnection(strConn)

dim strItem as String = "SELECT * FROM Products WHERE ProductID = " &
ID

dim Adapter as new SqlDataAdapter(strItem,objConn)
dim DataSet as new DataSet()
Adapter.Fill(DataSet, "ITEM")

Dim Description as String
Description = DataSet.Tables("ITEM").Columns("Description")
%>
<html> ..... </html>

Any help on this one please

Desmond.

Feb 22 '06 #2
Des
ID is not the Problem ID is an integer as it is an auto number. The
problem is with

Dim Description as String
Description = DataSet.Tables("ITEM").Columns("Description")

BC30311: Value of type 'System.Data.DataColumn' cannot be converted to
'String'.

Feb 22 '06 #3
HI
Here is the reason why its not working


DataTable userTable = new DataTable();

DataSet ds = new DataSet();

userTable.Columns.Add("userID", Type.GetType("System.Int32"));

userTable.Columns.Add("username", Type.GetType("System.String"));

DataRow userRow = userTable.NewRow();

userRow["userID"] = 1;

userRow["username"] = "Peter";

userTable.Rows.Add(userRow);

userRow = userTable.NewRow();

userRow["userID"] = 2;

userRow["username"] = "Paul";

userTable.Rows.Add(userRow);

userRow = userTable.NewRow();

userRow["userID"] = 3;

userRow["username"] = "Mary";

userTable.Rows.Add(userRow);

ds.Tables.Add(userTable);

MessageBox.Show(ds.Tables[0].Columns["username"] ); /**Compile error **/

/*Change the above code and try*/

MessageBox.Show(ds.Tables[0].Columns["username"].ToString() );

So change

Description = DataSet.Tables("ITEM").Columns("Description")
to

Description = DataSet.Tables("ITEM").Columns("Description").ToSt ring()

Cheers
Vinu

"Des" <de*********@aol.com> wrote in message news:11*********************@g14g2000cwa.googlegro ups.com...
ID is not the Problem ID is an integer as it is an auto number. The
problem is with

Dim Description as String
Description = DataSet.Tables("ITEM").Columns("Description")

BC30311: Value of type 'System.Data.DataColumn' cannot be converted to
'String'.

Feb 22 '06 #4
Des
No this isn't right I do not need to create a new table. I already have
one
ProdID is an auto number
Description is Text "Mary had a little lamb" why do I have to convert
text to text, if Description is a text field already.

Description = DataSet.Tables("ITEM").Columns("Description").ToSt ring()

Feb 22 '06 #5
Hi

Just change and try...let see whether it generating an error or not...i am
sure it won't try it...

Description = DataSet.Tables("ITEM").Columns("Description")
to

Description = DataSet.Tables("ITEM").Columns("Description").ToSt ring()

Vinu

"Des" <de*********@aol.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
No this isn't right I do not need to create a new table. I already have
one
ProdID is an auto number
Description is Text "Mary had a little lamb" why do I have to convert
text to text, if Description is a text field already.

Description = DataSet.Tables("ITEM").Columns("Description").ToSt ring()

Feb 22 '06 #6
Hi
The reason its not working is

statement DataSet.Tables("ITEM").Columns("Description") return DataColumn.
You can't assign DataColumn to String. That's why you need to use ToString
to convert to a string.

Vinu


"Des" <de*********@aol.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
No this isn't right I do not need to create a new table. I already have
one
ProdID is an auto number
Description is Text "Mary had a little lamb" why do I have to convert
text to text, if Description is a text field already.

Description = DataSet.Tables("ITEM").Columns("Description").ToSt ring()

Feb 22 '06 #7
Hi,

DataSet.Tables("ITEM").Columns("Description")

will not give you the coumn value, insted it will return the column name.To
get the column value

DataSet.Tables("ITEM").Rows[rowindex][columindex].ToString()

vinu

"Des" <de*********@aol.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
No this isn't right I do not need to create a new table. I already have
one
ProdID is an auto number
Description is Text "Mary had a little lamb" why do I have to convert
text to text, if Description is a text field already.

Description = DataSet.Tables("ITEM").Columns("Description").ToSt ring()

Feb 22 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Dariusz | last post: by
1 post views Thread by Google Mike | last post: by
2 posts views Thread by Dariusz | last post: by
reply views Thread by Andy | last post: by
2 posts views Thread by Ed | last post: by
7 posts views Thread by ianenis.tiryaki | last post: by
1 post views Thread by =?Utf-8?B?QWRyaWFuTW9ycmlz?= | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.