Friday, March 23, 2012

How to get the Data in Values?

hi

i have creat some code to get data from Database like:

Dim strNewsIDAsInteger = Request.QueryString("ID")

Dim NewsconnAsNew System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("SiteSQLServer").ToString())

Dim NewscmdAsNew System.Data.SqlClient.SqlCommand("SELECT FROM News (NewsGroupID, NewsTitle, News, NewsPosition) WHERE NewsID=strNewsID", Newsconn)

Now i wanna get Data in Values

Newsconn.Open()

Dim NewsGroupIDAsInteger = ???

Dim NewsTitleAsString = ???

Dim NewsAsString = ???

Dim NewsPositionAsInteger = ???

Please let me know how?

Newsconn.Close()

DrZ3D:

Dim NewscmdAsNew System.Data.SqlClient.SqlCommand("SELECT FROM News (NewsGroupID, NewsTitle, News, NewsPosition) WHERE NewsID=strNewsID", Newsconn)

I'm not a VB programmer, so someone else will have to help you with the VB syntax.

But the sql command is in a format I've never seen before.

I think you should be issuing this select statement:

"SELECTNewsGroupID, NewsTitle, News, NewsPositionFROM News WHERE ...

|||

you have an SQL connection and a querry.

So what you need now is a SQL Reader and then just loop throght this record set that is returned to get the values.

SqlDataReader readerAs new SqlCommand(query, conn)reader.ExecuteReader() if (reader.HasRows){// get the values here }
but are you getting 1 row returned or more?
|||

my conection is work and i get only one rowe from database

i only wana know how put the record in a value

thanks

|||

I'm not sure how will you provide the value forstrNewsID, so I'm leaving the problem on you. Other than that your modified code is below:

Dim strNewsIDAs Integer = Request.QueryString("ID")
Dim NewsconnAs New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("SiteSQLServer").ToString())

Dim NewscmdAs New System.Data.SqlClient.SqlCommand("SELECT NewsGroupID, NewsTitle, News, NewsPosition FROM News WHERE NewsID=strNewsID", Newsconn)

dim sdr as SqlDataReader

Dim NewsGroupIDAs Integer

Dim NewsTitleAs String

Dim NewsAs String

Dim NewsPositionAs Integer

Newsconn.Open()

sdr = Newscmd.executeReader()

if sdr.HasRows then

sdr.read()

NewsGroupID = sdr("NewsGroupID")
NewsTitle = sdr("NewsTitle")
News = sdr("News")
NewsPosition = sdr("NewsPosition")

end if

sdr.close()
newscommand.dispose()
Newsconn.Close()

Hope you will correct any syntactical errors.

Hope this will help.

No comments:

Post a Comment