Click here to Skip to main content
15,850,750 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello programmers, got a small problem here as you see i want to add a records on the database(same college, same code but different in centers).So when I click submit button 2 records are saved in my database
EX.
i put text on textboxes           Hidden variables of centers
college = wew                     123
code = wew                        456
centers = (based on hidden variables)
SUBMIT

And when I look on the database
College                 Code               Center
wew                     wew                123
wew                     wew                456

thats what i want, but got a problem. When I hit submit button an error occurs
Invalid object name 'tblTraningPlan'. pointing at command.executenonquery
Plss help me.. Did I forgot something?
Thanks and more power programmers
MY CODES:
VB
Imports System.Math
Imports System.Web
Imports System.Data.SqlClient
Imports System.Data
Partial Class Gee51
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        databind15029()
        databind12934()
    End Sub
    Public Sub databind15029()
        Dim strSQL As String
        Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        strSQL = "INSERT INTO tblTraningPlan(CostCenter, College, Category, CourseCode, CourseTitle, Duration) VALUES (@CostCenter, @College, @Category, @CourseCode, @CourseTitle, @Duration)"
        connection.Open()
        Dim command As New SqlCommand(strSQL, connection)
        command.Parameters.Add("@CostCenter", SqlDbType.NVarChar).Value = lbl15029.Text
        command.Parameters.Add("@College", SqlDbType.NVarChar).Value = txtCollege.Text
        command.Parameters.Add("@Category", SqlDbType.NVarChar).Value = lblGEE.Text
        command.Parameters.Add("@CourseCode", SqlDbType.NVarChar).Value = txtCCode.Text
        command.Parameters.Add("@CourseTitle", SqlDbType.NVarChar).Value = txtCT.Text
        command.Parameters.Add("@Duration", SqlDbType.NVarChar).Value = txtDuration.Text
        command.ExecuteNonQuery()
        connection.Close()
    End Sub
    Public Sub databind12934()
        Dim strSQL As String
        Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        strSQL = "INSERT INTO tblTraningPlan(CostCenter, College, Category, CourseCode, CourseTitle, Duration) VALUES (@CostCenter, @College, @Category, @CourseCode, @CourseTitle, @Duration)"
        connection.Open()
        Dim command As New SqlCommand(strSQL, connection)
        command.Parameters.Add("@CostCenter", SqlDbType.NVarChar).Value = lbl12934.Text
        command.Parameters.Add("@College", SqlDbType.NVarChar).Value = txtCollege.Text
        command.Parameters.Add("@Category", SqlDbType.NVarChar).Value = lblGEE.Text
        command.Parameters.Add("@CourseCode", SqlDbType.NVarChar).Value = txtCCode.Text
        command.Parameters.Add("@CourseTitle", SqlDbType.NVarChar).Value = txtCT.Text
        command.Parameters.Add("@Duration", SqlDbType.NVarChar).Value = txtDuration.Text
        command.ExecuteNonQuery()
        connection.Close()
    End Sub
End Class
Posted
Updated 28-Jun-11 16:24pm
v2

1 solution

With the exception of, what I assume are label controls (lbl12934 & lbl15029), both of these methods are the same. You should combine them rather than duplicating the objects. At the very least centralize the connection string and sql statement.

Have you verified the database is correct? The name is correct? DO you have access to it? Do you have insert rights?
 
Share this answer
 
Comments
janwel 28-Jun-11 21:38pm    
what do you mean sir.. Plss execuse my english but i cant understand plss make it simple ^_^
janwel 28-Jun-11 21:43pm    
Sir Got the problem tblTrainingPlan not TraningPlan ^_^
Got me a lot of time wasted thanks for the solution ^_^
janwel 28-Jun-11 21:44pm    
Sir what do you mean by combining them??
[no name] 28-Jun-11 22:19pm    
I meant that except for one thing, the label text, each of these methods has the same code. There is no reason to duplicate the code. Create one method and pass the text to it.
janwel 28-Jun-11 22:41pm    
sir is there any tutorial regarding this matter?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900