Reading an SQL database

D

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.
 
V

vinu

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
 
D

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'.
 
V

vinu

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").ToString()

Cheers
Vinu
 
D

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").ToString()
 
V

vinu

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").ToString()



Vinu
 
V

vinu

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
 
V

vinu

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top