Thursday, March 29, 2012

Looking to make "News" label from SQL

I'm trying to make a Label pull it's information from "tbl_news" on my SQL Server, how would I go about this?
its technically two labels, "newsdate" and "conews", where "conews" is the body of it. Any ideas?
-Jeff

There are several ways to go about this. You can use a SqlDataReader, a SqlDataAdapter and a DataSet, etc., etc. With a web app, you are generally better off using a SqlDataReader. For example, you could place the following code in your Page_Load event handler:

if (!Page.IsPostBack)
{
using (SqlConnection conn =new SqlConnection("server=(local);database=MyNewsDatabase;trusted connection=SSPI"))
{
SqlCommand cmd =new SqlCommand("SELECT newsdate, conews FROM tbl_news", conn);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
reader.Read();
this.conews.Text = Convert.ToString(reader["conews"]);
this.newsdate.Text = Convert.ToString(reader["newsdate"]);
reader.Close();
}
}


you need to know how to use the ADO.NET
ADO.NET can deal with all DB issues... you can google it or look for it in the MSDN and try to work it out

this is a sample ado.net code
cmd.CommandText= " select address from customer table"
cmd.CommandType= CommandType.Text
reader.ExcuteReader()
this is a code to read the data from the DB ... this is just a demo to give you a code sample i think your code should be similar after you add the conectionString ( this is the code that you need for assiging access to the database )......i hope that helps!!
herein lies the problem, I'm using dreamweaver, and coding in VB.
anything on how to do it with that?
-Jeff


The code will be fairly similar in VB.NET. The IDE you are using shouldn't affect how the code you write...

Iiesashima wrote:

herein lies the problem, I'm using dreamweaver, and coding in VB.
anything on how to do it with that?


.NET is .NET no matter what IDE you use. I would recommend that you first take a look at this sitehttp://authors.aspalliance.com/aspxtreme/adonet/index.aspx to learn how to pull information from a database.
Grz, Kris.
I understand this, I would rather use the labels I have since the page layout needs them the way they are.
Really, the only issue I have is the connection to the server. Ican do the conews.text = "" portions, and the select lines, I only needthe commands to make it actually connect.

anyone willing to help me make the connection?

If you need help with connection strings than visit www.connectionstrings.com
Let me know if you ahve other questions !

appreciated!
EDIT:
The connections aren't working...
code follows:
<%@. Page Language="VB" ContentType="text/html" ResponseEncoding="windows-1251" %>
Imports System.Data.SqlClient
<script language="vb" runat="server">
Dim oSQLConn As SqlConnection = New SqlConnection()
oSQLConn.ConnectionString="my connectionstring"
oSQLConn.Open()
Server=sql.wifieyeinc.com;Database=wifieyeinc;User ID=name;Password=password;Trusted_Connection=true
sub page_load
if Page.IsPostBack
dim myConn as new SqlConnection
String query = "select news_date, news_test from tbl_news";
SqlCommand myCmd = new SqlCommand ( query, myConn );
myConn.Open ( );
SqlDataReader myReader = myCmd.ExecuteReader ( );
end if
</script>
gives me this error:
BC30002: Type 'SqlConnection' is not defined.
any clues?

Iiesashima wrote:

appreciated!
oSQLConn.ConnectionString="my connectionstring"
oSQLConn.Open()
Server=sql.wifieyeinc.com;Database=wifieyeinc;User ID=sesshomaru;Password=predisposition;Trusted_Connection=true


where is "my connectionstring" defined?
i can see the definition of the connection string assigned for nothnig in your code!!!
where/how would I define this?


<%@. Page Language="VB" ContentType="text/html" ResponseEncoding="windows-1251" %>
<%@. Import namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
sub page_load()
if Page.IsPostBack'this means "if it IS a postback" - which means 'not the first time called'
Else'run this if first time page is called - fill the label - it will be filled by viewstate on postaback
Dim oSQLConn As SqlConnection = New SqlConnection()
dim MyConnectionString As String = "sql.wifieyeinc.com;Database=wifieyeinc;UserID=sesshomaru;Password=predisposition"
'when passing credentials - you don't specify "Trusted_Connection=true"
Dim Myquery As String = "select news_date, news_test from tbl_news"
Dim myCmd As SqlCommand = new SqlCommand ( Myquery,oSQLConn)
DimmyReader AsSqlDataReader = myCmd.ExecuteReader( )
end if
</script>
you might want to try putting the ip addres ofsql.wifieyeinc.com instead of the url
try that - you seem to be mixing C# syntax (; on end of line) with VB
Your Page attribute says VB
there are some goodtutorials that show how to retrieve data into adatareader
HTH

Iiesashima wrote:

Server=sql.wifieyeinc.com;Database=wifieyeinc;User ID=sesshomaru;Password=predisposition;Trusted_Connection=true


I hope that this isn't a live connectionstring with real usernames and passwords. Hackers love this kind of stuff. It's great that one who asks for help puts relevant information on the forums to get a decent answer but you should always be aware that you don't put valuable information on it!
Grz, Kris.
the sql address is real, but the username is nonsense.
i mean, c'mon, its from an anime.

0 comments:

Post a Comment