Tuesday, March 27, 2012

cant get data from sql

I'm adding a few simple pages to our existing website to be able to keep all of our vendor information. Most of the rest of the website is using a Microsoft Access database. This new portion though is being built using a SQL database. For some reason though I seem to be unable to pull any information from the sql database, I've added the sql connection string into the main web.config file, as well as importing all the needed namespaces. I don't get any errors when I view the pages, but also get nothing from the database. What might I need to look at for this issue, I'm confused about why I can't get any data from the db. Any help is greatly appreciated.

Hey,

What might the code you are using look like?

|||

here's my connection string in the web.config -- minus user id and password, and an example of how I'm trying to pull data

<

addname="SQLConnString"connectionString="Data Source=UELIIS;Initial Catalog=ITtech;Persist Security Info=True;User ID=*****;Password=*****"providerName="System.Data.SqlClient"/>

PrivateSub GetMaxVendorNum()

Dim NextVendorNumAsString

strSql =

"SELECT MAX(VendorNum) + 1 AS Expr1FROM Vendor"

dr = GetDataReader(strSql, strConn)

If dr.Read()Then

NextVendorNum = dr(

"Expr1").ToString

lblVendorNum.Text = NextVendorNum.ToString

EndIfEndSubPublicSharedFunction GetDataReader( _ByVal SQLStatementAsString, _ByVal ConnectionStringAsString)As SqlDataReaderDim drAs SqlDataReaderDim cmdAsNew SqlCommandWith cmd

.Connection = _

New SqlConnection(ConnectionString)

.Connection.Open()

.CommandText = SQLStatement

dr = .ExecuteReader(CommandBehavior.CloseConnection)

EndWithReturn drEndFunction

Thanks for your help

|||

Hey,

I tried your query, and that worked for me. The only thing I can think of is the fact that you are returning the data reader and something is happening in between. Is the problem that you are getting no entries or that you are getting an error thrown.

|||I'm just not getting any entries, just blank fields|||The really crazy thing is that on my first page I'm using a sqldatasource to populate a gridview and it pulls and displays the information with no problem. Its using the same connection string as my other pages.|||

well i've narrowed it down to the fact that my page_load event isn't firing for some reason, I even tried to put a script tag in the .aspx page and place it there instead of a code behind page but still nothing, can't even put text into the error label. Here's some of my code snippets that may help troubleshoot problem.

<%

@.PageLanguage="VB"AutoEventWireup="false"CodeFile="VendorEdit.aspx.vb"Inherits="VendorEdit" %>

Partial

Class VendorEdit

Inherits

System.Web.UI.Page

.... CODE.....

end class

Ask if you need to see any code or need more information. Thanks for your help.

|||

Hi,

Thanks for your code.

I tried it on my machine and it works fine. The only problem seems that I cannot see any code that your DataReader is binding to. Did you set it as the datasource of a GridView?

If so, please also call GridView.DataBind() after it has been set as the DataSource.

HTH.

|||

could you post your page_load event code make sure its something like following

Protected

Sub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.LoadEndSub

handles me.load is must.

thanks,

satish.

No comments:

Post a Comment