lz7cjc
asked on
Passing variables to a Stored Procedure from *.aspx
Hi
I have the following Sproc
Please can you tell me, from scratch (i.e opening the DB connection onwards) how i pass the parameters through to the Sproc
CREATE PROCEDURE proc_tblJob_Insert
(
@Jobname varchar(50),
@ConsultantID int,
@JobDescription varchar(50),
@statusID int,
@calldate datetime,
@comments varchar(50)
)
let me know if you need any more of the Sproc
thanks
Nick
I have the following Sproc
Please can you tell me, from scratch (i.e opening the DB connection onwards) how i pass the parameters through to the Sproc
CREATE PROCEDURE proc_tblJob_Insert
(
@Jobname varchar(50),
@ConsultantID int,
@JobDescription varchar(50),
@statusID int,
@calldate datetime,
@comments varchar(50)
)
let me know if you need any more of the Sproc
thanks
Nick
ASKER
ah...
in that case it might be a little more complicated!
I am getting the variables from a form like this:
Sub Submit_Click(Sender as Object, E as EventArgs)
GetNewJob(0)
Row = ClassyDS.Tables("Job").New Row
Row.Item("jobtitle") = title.Text
Row.Item("jobdescription") = description.Text
Row.Item("salary") = salary.Text
Row.Item("nextcall") = nextcall.Text
Row.Item("statusid") = ddlstatus.selecteditem.val ue
Row.Item("began") = beganDate.Text
GetNewCall(0)
Row = ClassyDS1.Tables("Call").N ewRow
' Row.Item("companyid") = ddlcompany.selecteditem.va lue
Row.Item("consultantID") = ddlperson.selecteditem.val ue
Row.Item("callDate") = beganDate.Text
Row.Item("Comments") = "Initiation Call"
Row.Item("JobId") = GetNewJob(CoNumSent)
ClassyDS.Tables("job").row s.add(row)
Adapter.Update(ClassyDS, "job")
ClassyDS1.Tables("call").r ows.add(ro w)
Adapter.Update(ClassyDS1, "call")
If ClassyDS.HasErrors then
Message.Text = "there was a an error in submitting the person's details. " & _
ClassyDS.Tables("job").Row s(0).Rower ror
Else
Message.text = "The person has been successfully entered"
title.Enabled = False
description.Enabled = False
ddlcompany.Enabled = False
salary.Enabled = False
ddlperson.Enabled = False
nextcall.Enabled = False
ddlstatus.Enabled = False
begandate.Enabled = False
End if
End Sub
Sub GetNewJob(CoNumSent as Integer)
SelectStatement = "Select * from tbl_job where jobid=" & CoNumSent
ConnectString = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=jobs;"
Connect.ConnectionString = ConnectString
Adapter.SelectCommand = new OLEDbCommand(SelectStateme nt, Connect)
ClassyCB = New OLEDBCommandBuilder(Adapte r)
Adapter.Fill(ClassyDS, "job")
Return CoNumSent
End Sub
Sub GetNewCall(CoNumSent as Integer)
SelectStatement = "Select * from tbl_call where callid=" & CoNumSent
ConnectString = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=jobs;"
Connect.ConnectionString = ConnectString
Adapter.SelectCommand = new OLEDbCommand(SelectStateme nt, Connect)
ClassyCB = New OLEDBCommandBuilder(Adapte r)
Adapter.Fill(ClassyDS1, "call")
End Sub
so where would i put this code and how would i assign the variables?
thanks
in that case it might be a little more complicated!
I am getting the variables from a form like this:
Sub Submit_Click(Sender as Object, E as EventArgs)
GetNewJob(0)
Row = ClassyDS.Tables("Job").New
Row.Item("jobtitle") = title.Text
Row.Item("jobdescription")
Row.Item("salary") = salary.Text
Row.Item("nextcall") = nextcall.Text
Row.Item("statusid") = ddlstatus.selecteditem.val
Row.Item("began") = beganDate.Text
GetNewCall(0)
Row = ClassyDS1.Tables("Call").N
' Row.Item("companyid") = ddlcompany.selecteditem.va
Row.Item("consultantID") = ddlperson.selecteditem.val
Row.Item("callDate") = beganDate.Text
Row.Item("Comments") = "Initiation Call"
Row.Item("JobId") = GetNewJob(CoNumSent)
ClassyDS.Tables("job").row
Adapter.Update(ClassyDS, "job")
ClassyDS1.Tables("call").r
Adapter.Update(ClassyDS1, "call")
If ClassyDS.HasErrors then
Message.Text = "there was a an error in submitting the person's details. " & _
ClassyDS.Tables("job").Row
Else
Message.text = "The person has been successfully entered"
title.Enabled = False
description.Enabled = False
ddlcompany.Enabled = False
salary.Enabled = False
ddlperson.Enabled = False
nextcall.Enabled = False
ddlstatus.Enabled = False
begandate.Enabled = False
End if
End Sub
Sub GetNewJob(CoNumSent as Integer)
SelectStatement = "Select * from tbl_job where jobid=" & CoNumSent
ConnectString = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=jobs;"
Connect.ConnectionString = ConnectString
Adapter.SelectCommand = new OLEDbCommand(SelectStateme
ClassyCB = New OLEDBCommandBuilder(Adapte
Adapter.Fill(ClassyDS, "job")
Return CoNumSent
End Sub
Sub GetNewCall(CoNumSent as Integer)
SelectStatement = "Select * from tbl_call where callid=" & CoNumSent
ConnectString = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=jobs;"
Connect.ConnectionString = ConnectString
Adapter.SelectCommand = new OLEDbCommand(SelectStateme
ClassyCB = New OLEDBCommandBuilder(Adapte
Adapter.Fill(ClassyDS1, "call")
End Sub
so where would i put this code and how would i assign the variables?
thanks
At what stage do you want to call the INSERT sproc?
I'm also confused as to why you're using two dataset. You ought to be using only one dataset, and populating the tables within in it.
ASKER
probably because i am pretty much new to this and don't really know what i am doing! have just been hacking code together until i can get it to work...
I want to load the page with a form containing text boxes and some drop down lists. I then want to hit the submit button, grab the values, write them to the DB via the Sproc and then display the values greyed out on the page (the same page)
so i guess i want to insert onClick and replace the code that i have... so do i just replace everything in onClick?
thanks
I want to load the page with a form containing text boxes and some drop down lists. I then want to hit the submit button, grab the values, write them to the DB via the Sproc and then display the values greyed out on the page (the same page)
so i guess i want to insert onClick and replace the code that i have... so do i just replace everything in onClick?
thanks
This ought to do it for you... (notice ConnectString is now a constant)
Don't forget, you'll have to refresh (i.e. repopulate) the DataSet, if you want it to reflect what is currently in the database after the update. Alternately, you can add rows directly to the tables...
Private Const ConnectString As String = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=jobs;"
Sub Submit_Click(ByVal Sender As Object, ByVal E As EventArgs)
Dim errorCode As String = tblJob_Insert(title.Text, ddlperson.selecteditem.val ue, description.Text, statusID, beganDate.Text, "Initiation Call")
If errorCode.Length <> 0 Then
Message.Text = "there was a an error in submitting the person's details. " & _
errorCode
Else
Message.text = "The person has been successfully entered"
title.Enabled = False
description.Enabled = False
ddlcompany.Enabled = False
salary.Enabled = False
ddlperson.Enabled = False
nextcall.Enabled = False
ddlstatus.Enabled = False
begandate.Enabled = False
End If
End Sub
Private Function tblJob_Insert(ByVal jobName As String, ByVal consultantID As Integer, ByVal jobDescription As String, ByVal statusID As Integer, ByVal calldate As DateTime, ByVal comments As String) As String
Try
Dim connection As New OleDb.OleDbConnection(Conn ectString)
connection.Open()
'create a command...
Dim command As New OleDb.OleDbCommand("proc_t blJob_Inse rt", connection)
command.CommandType = System.Data.CommandType.St oredProced ure
'parameters...
Dim jobNameParam As OleDb.OleDbParameter = command.Parameters.Add("@j obName", OleDb.OleDbType.VarChar, 50)
jobNameParam.Value = jobName
Dim consultantIDParam As OleDb.OleDbParameter = command.Parameters.Add("@C onsultantI D", OleDb.OleDbType.Integer)
consultantIDParam.Value = consultantID
Dim jobDescriptionParam As OleDb.OleDbParameter = command.Parameters.Add("@J obDescript ion", OleDb.OleDbType.VarChar, 50)
jobDescriptionParam.Value = jobDescription
Dim statusIDParam As OleDb.OleDbParameter = command.Parameters.Add("@s tatusID", OleDb.OleDbType.Integer)
statusIDParam.Value = statusID
Dim callDateParam As OleDb.OleDbParameter = command.Parameters.Add("@c alldate", OleDb.OleDbType.DBTimeStam p)
callDateParam.Value = calldate
Dim commentsParam As OleDb.OleDbParameter = command.Parameters.Add("@c omments", System.Data.SqlDbType.VarC har, 50)
commentsParam.Value = comments
' etc... add the remainder of your variables here
'execute...
command.ExecuteNonQuery()
'cleanup...
command.Dispose()
connection.Close()
Catch ex As Exception
tblJob_Insert = ex.Message
End Try
End Function
End Class
Don't forget, you'll have to refresh (i.e. repopulate) the DataSet, if you want it to reflect what is currently in the database after the update. Alternately, you can add rows directly to the tables...
Private Const ConnectString As String = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=jobs;"
Sub Submit_Click(ByVal Sender As Object, ByVal E As EventArgs)
Dim errorCode As String = tblJob_Insert(title.Text, ddlperson.selecteditem.val
If errorCode.Length <> 0 Then
Message.Text = "there was a an error in submitting the person's details. " & _
errorCode
Else
Message.text = "The person has been successfully entered"
title.Enabled = False
description.Enabled = False
ddlcompany.Enabled = False
salary.Enabled = False
ddlperson.Enabled = False
nextcall.Enabled = False
ddlstatus.Enabled = False
begandate.Enabled = False
End If
End Sub
Private Function tblJob_Insert(ByVal jobName As String, ByVal consultantID As Integer, ByVal jobDescription As String, ByVal statusID As Integer, ByVal calldate As DateTime, ByVal comments As String) As String
Try
Dim connection As New OleDb.OleDbConnection(Conn
connection.Open()
'create a command...
Dim command As New OleDb.OleDbCommand("proc_t
command.CommandType = System.Data.CommandType.St
'parameters...
Dim jobNameParam As OleDb.OleDbParameter = command.Parameters.Add("@j
jobNameParam.Value = jobName
Dim consultantIDParam As OleDb.OleDbParameter = command.Parameters.Add("@C
consultantIDParam.Value = consultantID
Dim jobDescriptionParam As OleDb.OleDbParameter = command.Parameters.Add("@J
jobDescriptionParam.Value = jobDescription
Dim statusIDParam As OleDb.OleDbParameter = command.Parameters.Add("@s
statusIDParam.Value = statusID
Dim callDateParam As OleDb.OleDbParameter = command.Parameters.Add("@c
callDateParam.Value = calldate
Dim commentsParam As OleDb.OleDbParameter = command.Parameters.Add("@c
commentsParam.Value = comments
' etc... add the remainder of your variables here
'execute...
command.ExecuteNonQuery()
'cleanup...
command.Dispose()
connection.Close()
Catch ex As Exception
tblJob_Insert = ex.Message
End Try
End Function
End Class
ASKER
:-) fantastic... thanks - i will cut and paste, fill in the missing bits and get back to you
ASKER
i get the following error:
BC30001: Statement is not valid in a namespace.
Line 239: #End ExternalSource
Line 240:
Line 241: Public Sub New()
Line 242: MyBase.New
Line 243: Dim dependencies As System.Collections.ArrayLi st
I have the following namespaces:
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OLE DB" %>
<%@ import Namespace="System.Data.Sql Client" %>
thanks
BC30001: Statement is not valid in a namespace.
Line 239: #End ExternalSource
Line 240:
Line 241: Public Sub New()
Line 242: MyBase.New
Line 243: Dim dependencies As System.Collections.ArrayLi
I have the following namespaces:
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OLE
<%@ import Namespace="System.Data.Sql
thanks
You don't need SQLClient... I misread your first code.
Other than that... I'm not at all sure what is is objecting to. You'd need to either post all the code, or check the stack in the error message.
Other than that... I'm not at all sure what is is objecting to. You'd need to either post all the code, or check the stack in the error message.
ASKER
this is all the info i have on the error:
this is the whole page...
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OLE DB" %>
<%@ import Namespace="System.Data.Sql Client" %>
<script runat="server">
Private Const ConnectString As String = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=jobs;"
Dim SelectStatement As String
Dim Connect as OLEDbConnection = New OLEDbConnection
Dim Adapter as OLEDBDataAdapter = New OLEDbDataAdapter
Dim ClassyCB As OLEDBCommandBuilder
Dim ClassyDS As Dataset = New DataSet
Dim ClassyDS1 As Dataset = New DataSet
Dim Row as DataRow
Sub Page_Load(Sender As Object, E As EventArgs)
Dim ConnectToDatabase as new SQLConnection("UID=SA;PWD= hello; Data Source=(local); database=jobs;")
Dim DataAdapterJob as new SQLDataAdapter("SELECT * FROM tbl_job",connecttodatabase )
Dim DataAdapterCall as new SQLDataAdapter("SELECT * FROM tbl_call",connecttodatabas e)
Dim DDLAdapterCompany as new SQLDataAdapter("SELECT CompanyName, CompanyID FROM tbl_Company",connecttodata base)
Dim DDLAdapterPeople as new SQLDataAdapter("SELECT consultantID, firstName, lastName FROM tbl_people",connecttodatab ase)
Dim DDLAdapterStatus as new SQLDataAdapter("SELECT statusId, statusName FROM tbl_status",connecttodatab ase)
Dim Ds as new DataSet
Dim Ds1 as new DataSet
Dim Ds2 as new DataSet
Dim Ds3 as new DataSet
Dim Ds4 as new DataSet
connecttodatabase.open
DataAdapterJob.Fill(DS, "Job")
DataAdapterCall.Fill(DS1, "Call")
DDLAdapterCompany.Fill(DS2 , "company")
DDLAdapterPeople.Fill(DS3, "People")
DDLAdapterStatus.Fill(DS4, "status")
connecttodatabase.close
JobGrid.Datasource = Ds.tables("job")
'company.Datasource = Ds3.tables("Company")
If Not IsPostBack Then
ddlcompany.DataSource = Ds2 '<--- (Dataset Name)
ddlcompany.DataMember = "company" '<--- (Table Name)
ddlcompany.DataTextField = "CompanyName" '<--- (Column Name)
ddlcompany.DataValueField = "companyid" '<--- (Column Name)
ddlperson.DataSource = Ds3 '<--- (Dataset Name)
ddlperson.DataMember = "People" '<--- (Table Name)
ddlperson.DataTextField = "lastname" '<--- (Column Name)
ddlperson.DataValueField = "consultantid" '<--- (Column Name)
ddlstatus.DataSource = Ds4 '<--- (Dataset Name)
ddlstatus.DataMember = "status" '<--- (Table Name)
ddlstatus.DataTextField = "statusName" '<--- (Column Name)
ddlstatus.DataValueField = "statusId" '<--- (Column Name)
end if
page.databind
end sub
Sub Submit_Click(ByVal Sender As Object, ByVal E As EventArgs)
Dim errorCode As String = tblJob_Insert(title.Text, ddlperson.selecteditem.val ue, description.Text, ddlstatus.selecteditem.val ue, beganDate.Text, "Initiation Call")
If errorCode.Length <> 0 Then
Message.Text = "there was a an error in submitting the person's details. " & _
errorCode
Else
Message.text = "The person has been successfully entered"
title.Enabled = False
description.Enabled = False
ddlcompany.Enabled = False
salary.Enabled = False
ddlperson.Enabled = False
nextcall.Enabled = False
ddlstatus.Enabled = False
begandate.Enabled = False
End If
End Sub
Private Function tblJob_Insert(ByVal jobName As String, ByVal consultantID As Integer, ByVal jobDescription As String, ByVal statusid As Integer, ByVal calldate As DateTime, ByVal comments As String) As String
Try
Dim connection As New OleDb.OleDbConnection(Conn ectString)
connection.Open()
'create a command...
'tbl_job parameters
Dim command As New OleDb.OleDbCommand("proc_t blJob_Inse rt", connection)
command.CommandType = System.Data.CommandType.St oredProced ure
'parameters...
Dim jobNameParam As OleDb.OleDbParameter = command.Parameters.Add("@j obName", OleDb.OleDbType.VarChar, 50)
jobNameParam.Value = title.text
Dim jobDescriptionParam As OleDb.OleDbParameter = command.Parameters.Add("@J obDescript ion", OleDb.OleDbType.VarChar, 50)
jobDescriptionParam.Value = description.Text
Dim statusIDParam As OleDb.OleDbParameter = command.Parameters.Add("@s tatusID", OleDb.OleDbType.Integer)
statusIDParam.Value = ddlstatus.selecteditem.val ue
'tbl_call parameters
Dim consultantIDParam As OleDb.OleDbParameter = command.Parameters.Add("@C onsultantI D", OleDb.OleDbType.Integer)
consultantIDParam.Value = ddlperson.selecteditem.val ue
Dim callDateParam As OleDb.OleDbParameter = command.Parameters.Add("@c alldate", OleDb.OleDbType.DBTimeStam p)
callDateParam.Value = nextcall.Text
Dim commentsParam As OleDb.OleDbParameter = command.Parameters.Add("@c omments", System.Data.SqlDbType.VarC har, 50)
commentsParam.Value = "initiation call"
' etc... add the remainder of your variables here
'execute...
command.ExecuteNonQuery()
'cleanup...
command.Dispose()
connection.Close()
Catch ex As Exception
tblJob_Insert = ex.Message
End Try
End Function
End Class
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:datagrid id="jobgrid" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black"></asp:da tagrid>
</p>
<table>
<tbody>
<tr>
<td>
Job Title
</td>
<td>
<asp:textbox id="title" runat="server" columns="50"></asp:textbox >
</td>
</tr>
<tr>
<td>
Job Description
</td>
<td>
<asp:textbox id="description" runat="server" columns="50" textmode="multiline" rows="5"></asp:textbox>
</td>
</tr>
<tr>
<td>
Company
</td>
<td>
<asp:DropDownList id="ddlcompany" runat="server"></asp:DropD ownList>
</td>
</tr>
<tr>
<td>
Salary
</td>
<td>
<asp:textbox id="Salary" runat="server" columns="50"></asp:textbox >
</td>
</tr>
<tr>
<td>
Consultant Name
</td>
<td>
<asp:DropDownList id="ddlperson" runat="server"></asp:DropD ownList>
</td>
</tr>
<tr>
<td>
Next Call
</td>
<td>
<asp:textbox id="nextCall" runat="server" columns="10"></asp:textbox >
</td>
</tr>
<tr>
<td>
Status
</td>
<td>
<asp:DropDownList id="ddlStatus" runat="server"></asp:DropD ownList>
</td>
</tr>
<tr>
<td>
Began Date
</td>
<td>
<asp:textbox id="begandate" runat="server" columns="10"></asp:textbox >
</td>
</tr>
<tr>
<td colspan="2">
<asp:button id="submit" onclick="Submit_Click" runat="server" text="Add New Person"></asp:button>
</td>
</tr>
<!-- Insert content here -->
</tbody>
</table>
<asp:Label id="message" runat="server" bold="true"></asp:Label>
</form>
</body>
</html>
thanks
this is the whole page...
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OLE
<%@ import Namespace="System.Data.Sql
<script runat="server">
Private Const ConnectString As String = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=jobs;"
Dim SelectStatement As String
Dim Connect as OLEDbConnection = New OLEDbConnection
Dim Adapter as OLEDBDataAdapter = New OLEDbDataAdapter
Dim ClassyCB As OLEDBCommandBuilder
Dim ClassyDS As Dataset = New DataSet
Dim ClassyDS1 As Dataset = New DataSet
Dim Row as DataRow
Sub Page_Load(Sender As Object, E As EventArgs)
Dim ConnectToDatabase as new SQLConnection("UID=SA;PWD=
Dim DataAdapterJob as new SQLDataAdapter("SELECT * FROM tbl_job",connecttodatabase
Dim DataAdapterCall as new SQLDataAdapter("SELECT * FROM tbl_call",connecttodatabas
Dim DDLAdapterCompany as new SQLDataAdapter("SELECT CompanyName, CompanyID FROM tbl_Company",connecttodata
Dim DDLAdapterPeople as new SQLDataAdapter("SELECT consultantID, firstName, lastName FROM tbl_people",connecttodatab
Dim DDLAdapterStatus as new SQLDataAdapter("SELECT statusId, statusName FROM tbl_status",connecttodatab
Dim Ds as new DataSet
Dim Ds1 as new DataSet
Dim Ds2 as new DataSet
Dim Ds3 as new DataSet
Dim Ds4 as new DataSet
connecttodatabase.open
DataAdapterJob.Fill(DS, "Job")
DataAdapterCall.Fill(DS1, "Call")
DDLAdapterCompany.Fill(DS2
DDLAdapterPeople.Fill(DS3,
DDLAdapterStatus.Fill(DS4,
connecttodatabase.close
JobGrid.Datasource = Ds.tables("job")
'company.Datasource = Ds3.tables("Company")
If Not IsPostBack Then
ddlcompany.DataSource = Ds2 '<--- (Dataset Name)
ddlcompany.DataMember = "company" '<--- (Table Name)
ddlcompany.DataTextField = "CompanyName" '<--- (Column Name)
ddlcompany.DataValueField = "companyid" '<--- (Column Name)
ddlperson.DataSource = Ds3 '<--- (Dataset Name)
ddlperson.DataMember = "People" '<--- (Table Name)
ddlperson.DataTextField = "lastname" '<--- (Column Name)
ddlperson.DataValueField = "consultantid" '<--- (Column Name)
ddlstatus.DataSource = Ds4 '<--- (Dataset Name)
ddlstatus.DataMember = "status" '<--- (Table Name)
ddlstatus.DataTextField = "statusName" '<--- (Column Name)
ddlstatus.DataValueField = "statusId" '<--- (Column Name)
end if
page.databind
end sub
Sub Submit_Click(ByVal Sender As Object, ByVal E As EventArgs)
Dim errorCode As String = tblJob_Insert(title.Text, ddlperson.selecteditem.val
If errorCode.Length <> 0 Then
Message.Text = "there was a an error in submitting the person's details. " & _
errorCode
Else
Message.text = "The person has been successfully entered"
title.Enabled = False
description.Enabled = False
ddlcompany.Enabled = False
salary.Enabled = False
ddlperson.Enabled = False
nextcall.Enabled = False
ddlstatus.Enabled = False
begandate.Enabled = False
End If
End Sub
Private Function tblJob_Insert(ByVal jobName As String, ByVal consultantID As Integer, ByVal jobDescription As String, ByVal statusid As Integer, ByVal calldate As DateTime, ByVal comments As String) As String
Try
Dim connection As New OleDb.OleDbConnection(Conn
connection.Open()
'create a command...
'tbl_job parameters
Dim command As New OleDb.OleDbCommand("proc_t
command.CommandType = System.Data.CommandType.St
'parameters...
Dim jobNameParam As OleDb.OleDbParameter = command.Parameters.Add("@j
jobNameParam.Value = title.text
Dim jobDescriptionParam As OleDb.OleDbParameter = command.Parameters.Add("@J
jobDescriptionParam.Value = description.Text
Dim statusIDParam As OleDb.OleDbParameter = command.Parameters.Add("@s
statusIDParam.Value = ddlstatus.selecteditem.val
'tbl_call parameters
Dim consultantIDParam As OleDb.OleDbParameter = command.Parameters.Add("@C
consultantIDParam.Value = ddlperson.selecteditem.val
Dim callDateParam As OleDb.OleDbParameter = command.Parameters.Add("@c
callDateParam.Value = nextcall.Text
Dim commentsParam As OleDb.OleDbParameter = command.Parameters.Add("@c
commentsParam.Value = "initiation call"
' etc... add the remainder of your variables here
'execute...
command.ExecuteNonQuery()
'cleanup...
command.Dispose()
connection.Close()
Catch ex As Exception
tblJob_Insert = ex.Message
End Try
End Function
End Class
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:datagrid id="jobgrid" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black"></asp:da
</p>
<table>
<tbody>
<tr>
<td>
Job Title
</td>
<td>
<asp:textbox id="title" runat="server" columns="50"></asp:textbox
</td>
</tr>
<tr>
<td>
Job Description
</td>
<td>
<asp:textbox id="description" runat="server" columns="50" textmode="multiline" rows="5"></asp:textbox>
</td>
</tr>
<tr>
<td>
Company
</td>
<td>
<asp:DropDownList id="ddlcompany" runat="server"></asp:DropD
</td>
</tr>
<tr>
<td>
Salary
</td>
<td>
<asp:textbox id="Salary" runat="server" columns="50"></asp:textbox
</td>
</tr>
<tr>
<td>
Consultant Name
</td>
<td>
<asp:DropDownList id="ddlperson" runat="server"></asp:DropD
</td>
</tr>
<tr>
<td>
Next Call
</td>
<td>
<asp:textbox id="nextCall" runat="server" columns="10"></asp:textbox
</td>
</tr>
<tr>
<td>
Status
</td>
<td>
<asp:DropDownList id="ddlStatus" runat="server"></asp:DropD
</td>
</tr>
<tr>
<td>
Began Date
</td>
<td>
<asp:textbox id="begandate" runat="server" columns="10"></asp:textbox
</td>
</tr>
<tr>
<td colspan="2">
<asp:button id="submit" onclick="Submit_Click" runat="server" text="Add New Person"></asp:button>
</td>
</tr>
<!-- Insert content here -->
</tbody>
</table>
<asp:Label id="message" runat="server" bold="true"></asp:Label>
</form>
</body>
</html>
thanks
ASKER
i know i haven't included all the parameters yet - i just wanted to get it working first
thanks
thanks
Is your database SQL server or something else? If it's SQL Server, we should only be using SQLClient, not OLEDB. If it's anything else, we should only be using OLEDB, not SQLClient. However... there are both in your code.
ASKER
it is SQL Server
Okay... I've separated your HTML and VB code. (This is easy to amintain isf you're using Visual Studio...) The following code goes in your HTML page (yourFormName.aspx). You'll have to replace yourFormName and YourProjectName appropriately
<%@ Page Language="VB" Debug="true" Codebehind="Yourformname.a spx.vb" Inherits="**yourProjectNam e.yourForm Name" %>
<HTML>
<HEAD>
</HEAD>
<body>
<form runat="server" ID="Form1">
<p>
<asp:datagrid id="jobgrid" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White"
ForeColor="Black"></asp:da tagrid>
</p>
<table>
<tbody>
<tr>
<td>
Job Title
</td>
<td>
<asp:textbox id="title" runat="server" columns="50"></asp:textbox >
</td>
</tr>
<tr>
<td>
Job Description
</td>
<td>
<asp:textbox id="description" runat="server" columns="50" textmode="multiline" rows="5"></asp:textbox>
</td>
</tr>
<tr>
<td>
Company
</td>
<td>
<asp:DropDownList id="ddlcompany" runat="server"></asp:DropD ownList>
</td>
</tr>
<tr>
<td>
Salary
</td>
<td>
<asp:textbox id="Salary" runat="server" columns="50"></asp:textbox >
</td>
</tr>
<tr>
<td>
Consultant Name
</td>
<td>
<asp:DropDownList id="ddlperson" runat="server"></asp:DropD ownList>
</td>
</tr>
<tr>
<td>
Next Call
</td>
<td>
<asp:textbox id="nextCall" runat="server" columns="10"></asp:textbox >
</td>
</tr>
<tr>
<td>
Status
</td>
<td>
<asp:DropDownList id="ddlStatus" runat="server"></asp:DropD ownList>
</td>
</tr>
<tr>
<td>
Began Date
</td>
<td>
<asp:textbox id="begandate" runat="server" columns="10"></asp:textbox >
</td>
</tr>
<tr>
<td colspan="2">
<asp:button id="submit" onclick="Submit_Click" runat="server" text="Add New Person"></asp:button>
</td>
</tr>
<!-- Insert content here -->
</tbody>
</table>
<asp:Label id="message" runat="server" bold="true"></asp:Label>
</form>
</body>
</HTML>
This code goes in a file called YourPageName.aspx.vb
Imports System.Data.OleDb
Imports System.Data.sqlclient
Public Class YourFormName
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
Protected WithEvents jobgrid As System.Web.UI.WebControls. DataGrid
Protected WithEvents title As System.Web.UI.WebControls. TextBox
Protected WithEvents description As System.Web.UI.WebControls. TextBox
Protected WithEvents ddlcompany As System.Web.UI.WebControls. DropDownLi st
Protected WithEvents Salary As System.Web.UI.WebControls. TextBox
Protected WithEvents ddlperson As System.Web.UI.WebControls. DropDownLi st
Protected WithEvents nextCall As System.Web.UI.WebControls. TextBox
Protected WithEvents ddlStatus As System.Web.UI.WebControls. DropDownLi st
Protected WithEvents begandate As System.Web.UI.WebControls. TextBox
Protected WithEvents submit As System.Web.UI.WebControls. Button
Protected WithEvents message As System.Web.UI.WebControls. Label
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Const ConnectString As String = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=jobs;"
Dim SelectStatement As String
Dim Connect As OLEDbConnection = New OLEDbConnection
Dim Adapter As OLEDBDataAdapter = New OLEDbDataAdapter
Dim ClassyCB As OLEDBCommandBuilder
Dim ClassyDS As DataSet = New DataSet
Dim ClassyDS1 As DataSet = New DataSet
Dim Row As DataRow
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
Dim ConnectToDatabase As New SQLConnection("UID=SA;PWD= hello; Data Source=(local); database=jobs;")
Dim DataAdapterJob As New SQLDataAdapter("SELECT * FROM tbl_job", ConnectToDatabase)
Dim DataAdapterCall As New SQLDataAdapter("SELECT * FROM tbl_call", ConnectToDatabase)
Dim DDLAdapterCompany As New SQLDataAdapter("SELECT CompanyName, CompanyID FROM tbl_Company", ConnectToDatabase)
Dim DDLAdapterPeople As New SQLDataAdapter("SELECT consultantID, firstName, lastName FROM tbl_people", ConnectToDatabase)
Dim DDLAdapterStatus As New SQLDataAdapter("SELECT statusId, statusName FROM tbl_status", ConnectToDatabase)
Dim Ds As New DataSet
Dim Ds1 As New DataSet
Dim Ds2 As New DataSet
Dim Ds3 As New DataSet
Dim Ds4 As New DataSet
ConnectToDatabase.open()
DataAdapterJob.Fill(Ds, "Job")
DataAdapterCall.Fill(Ds1, "Call")
DDLAdapterCompany.Fill(Ds2 , "company")
DDLAdapterPeople.Fill(Ds3, "People")
DDLAdapterStatus.Fill(Ds4, "status")
ConnectToDatabase.close()
jobgrid.DataSource = Ds.Tables("job")
'company.Datasource = Ds3.tables("Company")
If Not IsPostBack Then
ddlcompany.DataSource = Ds2 '<--- (Dataset Name)
ddlcompany.DataMember = "company" '<--- (Table Name)
ddlcompany.DataTextField = "CompanyName" '<--- (Column Name)
ddlcompany.DataValueField = "companyid" '<--- (Column Name)
ddlperson.DataSource = Ds3 '<--- (Dataset Name)
ddlperson.DataMember = "People" '<--- (Table Name)
ddlperson.DataTextField = "lastname" '<--- (Column Name)
ddlperson.DataValueField = "consultantid" '<--- (Column Name)
ddlStatus.DataSource = Ds4 '<--- (Dataset Name)
ddlStatus.DataMember = "status" '<--- (Table Name)
ddlStatus.DataTextField = "statusName" '<--- (Column Name)
ddlStatus.DataValueField = "statusId" '<--- (Column Name)
End If
Page.DataBind()
End Sub
Sub Submit_Click(ByVal Sender As Object, ByVal E As EventArgs)
Dim errorCode As String = tblJob_Insert(title.Text, ddlperson.SelectedItem.Val ue, description.Text, ddlStatus.SelectedItem.Val ue, begandate.Text, "Initiation Call")
If errorCode.Length <> 0 Then
message.Text = "there was a an error in submitting the person's details. " & _
errorCode
Else
message.Text = "The person has been successfully entered"
title.Enabled = False
description.Enabled = False
ddlcompany.Enabled = False
Salary.Enabled = False
ddlperson.Enabled = False
nextCall.Enabled = False
ddlStatus.Enabled = False
begandate.Enabled = False
End If
End Sub
Private Function tblJob_Insert(ByVal jobName As String, ByVal consultantID As Integer, ByVal jobDescription As String, ByVal statusid As Integer, ByVal calldate As DateTime, ByVal comments As String) As String
Try
Dim connection As New OleDb.OleDbConnection(Conn ectString)
connection.Open()
'create a command...
'tbl_job parameters
Dim command As New OleDb.OleDbCommand("proc_t blJob_Inse rt", connection)
command.CommandType = System.Data.CommandType.St oredProced ure
'parameters...
Dim jobNameParam As OleDb.OleDbParameter = command.Parameters.Add("@j obName", OleDb.OleDbType.VarChar, 50)
jobNameParam.Value = title.Text
Dim jobDescriptionParam As OleDb.OleDbParameter = command.Parameters.Add("@J obDescript ion", OleDb.OleDbType.VarChar, 50)
jobDescriptionParam.Value = description.Text
Dim statusIDParam As OleDb.OleDbParameter = command.Parameters.Add("@s tatusID", OleDb.OleDbType.Integer)
statusIDParam.Value = ddlStatus.SelectedItem.Val ue
'tbl_call parameters
Dim consultantIDParam As OleDb.OleDbParameter = command.Parameters.Add("@C onsultantI D", OleDb.OleDbType.Integer)
consultantIDParam.Value = ddlperson.SelectedItem.Val ue
Dim callDateParam As OleDb.OleDbParameter = command.Parameters.Add("@c alldate", OleDb.OleDbType.DBTimeStam p)
callDateParam.Value = nextCall.Text
Dim commentsParam As OleDb.OleDbParameter = command.Parameters.Add("@c omments", System.Data.SqlDbType.VarC har, 50)
commentsParam.Value = "initiation call"
' etc... add the remainder of your variables here
'execute...
command.ExecuteNonQuery()
'cleanup...
command.Dispose()
connection.Close()
Catch ex As Exception
Return ex.Message
End Try
End Function
End Class
<%@ Page Language="VB" Debug="true" Codebehind="Yourformname.a
<HTML>
<HEAD>
</HEAD>
<body>
<form runat="server" ID="Form1">
<p>
<asp:datagrid id="jobgrid" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White"
ForeColor="Black"></asp:da
</p>
<table>
<tbody>
<tr>
<td>
Job Title
</td>
<td>
<asp:textbox id="title" runat="server" columns="50"></asp:textbox
</td>
</tr>
<tr>
<td>
Job Description
</td>
<td>
<asp:textbox id="description" runat="server" columns="50" textmode="multiline" rows="5"></asp:textbox>
</td>
</tr>
<tr>
<td>
Company
</td>
<td>
<asp:DropDownList id="ddlcompany" runat="server"></asp:DropD
</td>
</tr>
<tr>
<td>
Salary
</td>
<td>
<asp:textbox id="Salary" runat="server" columns="50"></asp:textbox
</td>
</tr>
<tr>
<td>
Consultant Name
</td>
<td>
<asp:DropDownList id="ddlperson" runat="server"></asp:DropD
</td>
</tr>
<tr>
<td>
Next Call
</td>
<td>
<asp:textbox id="nextCall" runat="server" columns="10"></asp:textbox
</td>
</tr>
<tr>
<td>
Status
</td>
<td>
<asp:DropDownList id="ddlStatus" runat="server"></asp:DropD
</td>
</tr>
<tr>
<td>
Began Date
</td>
<td>
<asp:textbox id="begandate" runat="server" columns="10"></asp:textbox
</td>
</tr>
<tr>
<td colspan="2">
<asp:button id="submit" onclick="Submit_Click" runat="server" text="Add New Person"></asp:button>
</td>
</tr>
<!-- Insert content here -->
</tbody>
</table>
<asp:Label id="message" runat="server" bold="true"></asp:Label>
</form>
</body>
</HTML>
This code goes in a file called YourPageName.aspx.vb
Imports System.Data.OleDb
Imports System.Data.sqlclient
Public Class YourFormName
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
Protected WithEvents jobgrid As System.Web.UI.WebControls.
Protected WithEvents title As System.Web.UI.WebControls.
Protected WithEvents description As System.Web.UI.WebControls.
Protected WithEvents ddlcompany As System.Web.UI.WebControls.
Protected WithEvents Salary As System.Web.UI.WebControls.
Protected WithEvents ddlperson As System.Web.UI.WebControls.
Protected WithEvents nextCall As System.Web.UI.WebControls.
Protected WithEvents ddlStatus As System.Web.UI.WebControls.
Protected WithEvents begandate As System.Web.UI.WebControls.
Protected WithEvents submit As System.Web.UI.WebControls.
Protected WithEvents message As System.Web.UI.WebControls.
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Const ConnectString As String = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=jobs;"
Dim SelectStatement As String
Dim Connect As OLEDbConnection = New OLEDbConnection
Dim Adapter As OLEDBDataAdapter = New OLEDbDataAdapter
Dim ClassyCB As OLEDBCommandBuilder
Dim ClassyDS As DataSet = New DataSet
Dim ClassyDS1 As DataSet = New DataSet
Dim Row As DataRow
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
Dim ConnectToDatabase As New SQLConnection("UID=SA;PWD=
Dim DataAdapterJob As New SQLDataAdapter("SELECT * FROM tbl_job", ConnectToDatabase)
Dim DataAdapterCall As New SQLDataAdapter("SELECT * FROM tbl_call", ConnectToDatabase)
Dim DDLAdapterCompany As New SQLDataAdapter("SELECT CompanyName, CompanyID FROM tbl_Company", ConnectToDatabase)
Dim DDLAdapterPeople As New SQLDataAdapter("SELECT consultantID, firstName, lastName FROM tbl_people", ConnectToDatabase)
Dim DDLAdapterStatus As New SQLDataAdapter("SELECT statusId, statusName FROM tbl_status", ConnectToDatabase)
Dim Ds As New DataSet
Dim Ds1 As New DataSet
Dim Ds2 As New DataSet
Dim Ds3 As New DataSet
Dim Ds4 As New DataSet
ConnectToDatabase.open()
DataAdapterJob.Fill(Ds, "Job")
DataAdapterCall.Fill(Ds1, "Call")
DDLAdapterCompany.Fill(Ds2
DDLAdapterPeople.Fill(Ds3,
DDLAdapterStatus.Fill(Ds4,
ConnectToDatabase.close()
jobgrid.DataSource = Ds.Tables("job")
'company.Datasource = Ds3.tables("Company")
If Not IsPostBack Then
ddlcompany.DataSource = Ds2 '<--- (Dataset Name)
ddlcompany.DataMember = "company" '<--- (Table Name)
ddlcompany.DataTextField = "CompanyName" '<--- (Column Name)
ddlcompany.DataValueField = "companyid" '<--- (Column Name)
ddlperson.DataSource = Ds3 '<--- (Dataset Name)
ddlperson.DataMember = "People" '<--- (Table Name)
ddlperson.DataTextField = "lastname" '<--- (Column Name)
ddlperson.DataValueField = "consultantid" '<--- (Column Name)
ddlStatus.DataSource = Ds4 '<--- (Dataset Name)
ddlStatus.DataMember = "status" '<--- (Table Name)
ddlStatus.DataTextField = "statusName" '<--- (Column Name)
ddlStatus.DataValueField = "statusId" '<--- (Column Name)
End If
Page.DataBind()
End Sub
Sub Submit_Click(ByVal Sender As Object, ByVal E As EventArgs)
Dim errorCode As String = tblJob_Insert(title.Text, ddlperson.SelectedItem.Val
If errorCode.Length <> 0 Then
message.Text = "there was a an error in submitting the person's details. " & _
errorCode
Else
message.Text = "The person has been successfully entered"
title.Enabled = False
description.Enabled = False
ddlcompany.Enabled = False
Salary.Enabled = False
ddlperson.Enabled = False
nextCall.Enabled = False
ddlStatus.Enabled = False
begandate.Enabled = False
End If
End Sub
Private Function tblJob_Insert(ByVal jobName As String, ByVal consultantID As Integer, ByVal jobDescription As String, ByVal statusid As Integer, ByVal calldate As DateTime, ByVal comments As String) As String
Try
Dim connection As New OleDb.OleDbConnection(Conn
connection.Open()
'create a command...
'tbl_job parameters
Dim command As New OleDb.OleDbCommand("proc_t
command.CommandType = System.Data.CommandType.St
'parameters...
Dim jobNameParam As OleDb.OleDbParameter = command.Parameters.Add("@j
jobNameParam.Value = title.Text
Dim jobDescriptionParam As OleDb.OleDbParameter = command.Parameters.Add("@J
jobDescriptionParam.Value = description.Text
Dim statusIDParam As OleDb.OleDbParameter = command.Parameters.Add("@s
statusIDParam.Value = ddlStatus.SelectedItem.Val
'tbl_call parameters
Dim consultantIDParam As OleDb.OleDbParameter = command.Parameters.Add("@C
consultantIDParam.Value = ddlperson.SelectedItem.Val
Dim callDateParam As OleDb.OleDbParameter = command.Parameters.Add("@c
callDateParam.Value = nextCall.Text
Dim commentsParam As OleDb.OleDbParameter = command.Parameters.Add("@c
commentsParam.Value = "initiation call"
' etc... add the remainder of your variables here
'execute...
command.ExecuteNonQuery()
'cleanup...
command.Dispose()
connection.Close()
Catch ex As Exception
Return ex.Message
End Try
End Function
End Class
ASKER
crikey... thanks
I am using WebMatrix... will that make any difference?
i can that i need to create two pages here:
Yourformname.aspx.vb
YourPageName.aspx.vb
but i don't have a project to reference????
thanks
I am using WebMatrix... will that make any difference?
i can that i need to create two pages here:
Yourformname.aspx.vb
YourPageName.aspx.vb
but i don't have a project to reference????
thanks
I've never used WebMatrix... does it support code-behind pages?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
fantastic - you are amazing...
that works an absolute treat
thanks very much
that works an absolute treat
thanks very much
Public Sub tblJob_Insert(jobName as string) ' Add the remainder of your parameters here
Dim connection As SqlConnection = New SqlConnection("YourConnect
connection.Open
'create a command...
Dim command As SqlCommand = New SqlCommand("proc_tblJob_In
command.CommandType = System.Data.CommandType.St
'parameters...
Dim jobNameParam As SqlParameter = command.Parameters.Add("@j
jobNameParam.Value = jobName
' etc... add the remainder of your variables here
'execute...
command.ExecuteNonQuery
'cleanup...
command.Dispose
connection.Close
End Sub