Archive for April, 2008

ASP.net mySQL Insert function

This code is from a football website off a Add_Player page. The code will add a new football player to the database.

The table the code is added to contains:

ID,Name, Team, DOB, Bio, Position, SquadNo, Birthplace

As ID is set to auto increment within the database then it is not required in the insert.

I have placed some very basic error checking, the TRY CATCH will attempt to insert the data, outputing to a label if it is successful or not.

code:

Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim myConnection As MySqlConnection
        Dim myDataAdapter As MySqlDataAdapter
        Dim myDataSet As DataSet

        myConnection = New MySqlConnection(”server=mysql.server.com; user id=Admin; password=xxx; database=data1; pooling=false;”)
        Dim myExecuteQuery = “INSERT INTO Players (Name, Team, DOB, Bio, Position, SquadNo, Birthplace) VALUES ( ‘” & txtname.Text & “‘ , ‘” & listTeam.selecteditem.value & “‘, ‘” & txtDOB.text & “‘  , ‘” & txtbio.text & “‘ ,’” & listPos.selecteditem.value & “‘ ,’” & txtsquadno.text & “‘,’” & txtbirth.text & “‘ )”

        Dim myCommand As New MySqlCommand(myExecuteQuery, myConnection)
        myCommand.Connection.Open()

        Try
            myCommand.ExecuteNonQuery()
            myConnection.Close()
            lblmsg.text = “Player has been added ”

        Catch
            lblmsg.text = “Could not insert please resubmit”
        End Try
    End Sub

ASP.net mySQL connection

If you are using ASP.net and would like to connect to a mySQL database read on. Before you can use mySQL with ASP.net you will need to install the connector:

http://dev.mysql.com/downloads/connector/net/1.0.html

MySql.Data.dll needs to be placed within you BIN folder. 

The sub routine below will connect to a mySQL database and pull data from:

database “data1″
table “posts”
Where forumid = variable
Ordered by dateposted which is a date column

 The data is then binded to “Repeater1″.

Server can be localhost or the address of the mySQL server
UserID and password are required

code:

Sub GetPosts()

        Dim myConnection As MySqlConnection
        Dim myDataAdapter As MySqlDataAdapter
        Dim myDataSet As DataSet

        Dim strSQL As String
        Dim iRecordCount As Integer

        myConnection = New MySqlConnection(”server=mysql.provider.com; user id=SQLAdmin; password=xxxxx; database=data1; pooling=false;”)

        strSQL = “SELECT * FROM Posts where forumid=’” & forum & “‘ order by dateposted desc”

        myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
        myDataSet = New Dataset()
        myDataAdapter.Fill(myDataSet, “Posts”)

        Dim clientview As New DataView(myDataSet.Tables(”Posts”))

        Repeater1.DataSource = clientview
        Repeater1.databind()

    End Sub

JavaScript Check Screen Resolution

If you have a ASP.NET webpage and want to check the users screen resolution then display the correctly sized image the following code will help (Goes in the head of the page):

<SCRIPT type=”text/javascript”>if(screen.width >= 1600) { document.write(“<style type=’text/css’> body { background-image: url(’background1600.jpg’); background-repeat: no-repeat; background-attachment: fixed; } </style>”); }

else if (screen.width == 1280) { document.write(”<style type=’text/css’> body { background-image: url(’background1280.jpg’);background-repeat: no-repeat; background-attachment: fixed; } </style>”); }

else if (screen.width == 1024) { document.write(“<style type=’text/css’> body { background-image: url(’background1024.jpg’); background-repeat: no-repeat; background-attachment: fixed; } </style>”); }else if (screen.width == 800) { document.write(”<style type=’text/css’> body { background-image: url(’background800.jpg’); background-repeat: no-repeat; background-attachment: fixed;} </style>”); }

else { document.write(“<style type=’text/css’> body { background-image: url(’background1280.jpg’); background-repeat: no-repeat; background-attachment: fixed; } </style>”); }</SCRIPT>

Copy and past this code into notepad before your code editor:

Working example of code here:

 http://www.lervy.net/australia/