Private Sub TbDepot_KeyUp(sender As Object, e As KeyEventArgs) Handles TbDepot.KeyUp
con = New SqlConnection("Data Source=USER\SQLEXPRESS;Initial Catalog=Sap;Integrated Security=True")
con.Open()
Dim cmd0 As New SqlCommand("select * from dbo.Depot where Code=#code", con)
cmd0.Parameters.AddWithValue("#code", TbDepot.Text)
Dim dr As SqlDataReader
dr = cmd0.ExecuteReader()
If (dr.Read()) Then
LDepot.Text = dr.Item("Name")
dr.Close()
cmd1 = New SqlCommand("Select * from dbo.Item", con)
'cmd1.Parameters.AddWithValue("#depot", TbDepot.Text)
da1 = New SqlDataAdapter(cmd1)
dt1 = New DataTable()
da1.Fill(dt1)
GvRates.DataSource = dt1
Else
MessageBox.Show("Please type the correct entry")
TbDepot.Clear()
End If
con.Close()
End Sub
Please check the following code.
Related
GM everybody
i'm finding some issues in the implementation of my shopping cart
The query works but it doesn't store information in session.
Will i fix the query or the problem is the session ?
Here the code :
Dim constr As String = ConfigurationManager.ConnectionStrings("!aCommerce-ConnectionString!").ConnectionString
' Query SQL
Using cmd As New SqlCommand("SELECT Id,NomeProdotto, PrezzoProdotto, Quantità FROM aProdotti WHERE ID='" + Request.QueryString("ID").ToString + "' OR ID='" + Request.QueryString("ID").ToString + "'")
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
Session("dt") = dt
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
change you code to below
Dim constr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim con As New SqlConnection()
con.ConnectionString = constr
con.Open()
' Query SQL
Using cmd As New SqlCommand("SELECT Id,PromoCode as NomeProdotto,PromoCodeMessage as PrezzoProdotto, PromoCodeLimit as Quantità FROM EventPromocodetbl WHERE ID=" & Request.QueryString("ID").ToString & "", con)
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
Dim SessionDt As New DataTable()
SessionDt = Session("dt")
If Not SessionDt Is Nothing Then
If (dt.Rows.Count > 0) Then
Dim dr As DataRow
dr = SessionDt.NewRow()
dr("Id") = dt.Rows(0)("Id").ToString()
dr("NomeProdotto") = dt.Rows(0)("NomeProdotto").ToString
dr("PrezzoProdotto") = dt.Rows(0)("PrezzoProdotto").ToString
dr("Quantità") = dt.Rows(0)("Quantità").ToString
SessionDt.Rows.Add(dr)
End If
Session("dt") = SessionDt
GridView1.DataSource = SessionDt
GridView1.DataBind()
Else
Session("dt") = dt
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Using
End Using
Problem is you are assigning blank DatatTable to session and then you are filling that DataTable using DataAdapter fill method that is issue.
How can I call a stored procedure in the following code instead if the sqlcommand statement,
<WebMethod> _
Public Function GetPtPrt() As String
Dim constr As String = ConfigurationManager.ConnectionStrings("ARTSQLConStrng").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT TblRegjoin.PrtFilenum, TblReg.Fname + ' ' + TblReg.Sname + ' ' + TblReg.Lname, PrtStatus FROM TblReg INNER JOIN TblRegjoin ON TblReg.Filenum = TblRegjoin.PrtFilenum WHERE (TblRegjoin.PtFilenum = 15090248) ORDER BY TblRegjoin.PrtFilenum")
cmd.Connection = con
Dim ds As New DataSet()
Using sda As New SqlDataAdapter(cmd)
sda.Fill(ds, "PtPrt")
End Using
Return ds.GetXml()
End Using
End Using
End Function
Try This:
<WebMethod> _
Public Function GetPtPrt() As String
Dim constr As String = ConfigurationManager.ConnectionStrings("ARTSQLConStrng").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("ProcedureName", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = con
Dim ds As New DataSet()
Using sda As New SqlDataAdapter(cmd)
sda.Fill(ds, "PtPrt")
End Using
Return ds.GetXml()
End Using
End Using
End Function
Hope it helps.
I am working on asp.net using VB and SQL database
I have two tables mst_Emp & mst_dept
mst_dept got following columns (dpt_ID(PK),dpt_name,dpt_descrip)
mst_Emp got following columns (Emp_ID,Emp_FirstName,Emp_LastName,Emp_Address,Emp_ContactNo,Dept_ID(Foreign key),Marital_Status,Gender)
Now I have a Employee Detail Form in that I have Department Name label(DataBind from mst_Dept) and I have DropDownList for that to select. If some choose value from DropDownList I want it to get stored by Dept_ID in database. How can I do that ?
Try binding your Department Dropdownlist like
Using sqlconn As New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString)
If sqlconn.State = ConnectionState.Closed Then
sqlconn.Open()
End If
Dim ds As New DataSet()
Dim qry As String = "Select dpt_ID,dpt_name from mst_Dept"
Using cmd As New SqlCommand(qry, sqlconn)
Dim sda As New SqlDataAdapter(cmd)
sda.Fill(ds)
D_ddlDepartment.DataSource = ds
D_ddlDepartment.DataValueField = "dpt_ID"
D_ddlDepartment.DataTextField = "dpt_name"
D_ddlDepartment.DataBind()
D_ddlDepartment.Items.Insert(0, "-- Select Department --")
If sqlconn.State = ConnectionState.Open Then
sqlconn.Close()
End If
End Using
End Using
and you can access the Dept_id like
Dim Deptid As Integer = Convert.ToInt32(D_ddlDepartment.SelectedValue)
FINALLY GOT IT THANKS :-)
Dim ds As New DataSet
Dim cmd1 As SqlCommand = New SqlCommand()
Dim sqlconn As SqlConnection = New SqlConnection()
sqlconn.ConnectionString = "Data Source=PRGM\SQLEXPRESS;Initial Catalog=HRMS;Integrated Security=True"
sqlconn.Open()
cmd1 = New SqlCommand("select Dpt_ID,Dpt_Name from mst_Dept", sqlconn)
'Dim qry As String = "select Dpt_ID,Dpt_Name from mst_Dept"
'cmd1 As New SqlCommand(qry, sqlconn)
Dim sda As New SqlDataAdapter(cmd1)
sda.Fill(ds)
DropDownList1.DataSource = ds.Tables(0)
DropDownList1.DataValueField = ds.Tables(0).Columns("Dpt_ID").ColumnName
DropDownList1.DataTextField = ds.Tables(0).Columns("Dpt_Name").ColumnName
DropDownList1.DataBind()
DropDownList1.Items.Insert(0, "-- Select Department --")
sqlconn.Close()
End If
I write a code to retrieve to show data from Stored Procedure with ASP.NET like this
Private Sub AutoNumberVerification()
Dim Conn As New SqlConnection(strConn.ToString)
Dim cmd As New SqlCommand()
Try
cmd.Connection = Conn
cmd.CommandType = CommandType.StoredProcedure
Conn.Open()
If (txtProducerID.Text.Trim.Length = 9) Then
cmd.CommandText = "spProductCount"
cmd.Parameters.AddWithValue("#ID", txtProducerID.Text)
End If
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
Dim dr As SqlDataReader = cmd.ExecuteReader()
dt.Load(dr)
If (convertInteger(dt.Rows(0)("estbProducerID").ToString) > 0) Then
cmd.CommandText = "spProductCount"
rdoEstbProducerList.Items.FindByValue(convertInteger(dt.Rows(0)("estbProducerID"))).Selected = True
End If
cmd.CommandText = "spProductCount"
TextBox11.Text = dt.Rows(0)("ALL").ToString
TextBox4.Text = dt.Rows(0)("FOOD").ToString
TextBox5.Text = dt.Rows(0)("DRINKS").ToString
TextBox6.Text = dt.Rows(0)("TAILOR").ToString
TextBox7.Text = dt.Rows(0)("USABLE").ToString
TextBox8.Text = dt.Rows(0)("HERB").ToString
cmd.CommandText = "spProductproveCounts"
TextBox12.Text = dt.Rows(0)("REGISTERED").ToString
Catch ex As Exception
Response.Write("ERROR Load: " & ex.Message)
Finally
Conn.Close()
End Try
End Sub
but it show an error = "REGISTERED" is not belong to the table
how can I use two stored procedures?
Just setting the cmd.CommandText to a stored procedure name will not execute it. You still need to call ExecuteReader after each change to the CommandText
Whenever I try this code the page remains refreshing.
Imports System.Data.SqlClient
Imports System.Data
Partial Class ProjectReport
Inherits System.Web.UI.Page
Private myTotal As Decimal = 0
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load, Chart1.Load
Dim ProjectID As Integer = Session("project_id")
Session("ProjectID") = ProjectID
lblProjNameHeading.Text = "[ " + ProjectID.ToString + " ]"
Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
Dim projComm As String = "SELECT project_id, project_start, project_finish, project_budget, project_cost FROM projects WHERE project_id=#parameter"
Dim projSQL As New SqlCommand
conn.Open()
projSQL = New SqlCommand(projComm, conn)
projSQL.Parameters.AddWithValue("#parameter", ProjectID.ToString)
Dim datareader As SqlDataReader = projSQL.ExecuteReader()
While datareader.Read
lblProjectCode.Text = datareader("project_id").ToString
lblProjectStart.Text = datareader("project_start").ToString
lblProjectStart2.Text = datareader("project_start").ToString
lblProjectEnd.Text = datareader("project_finish").ToString
lblProjectEnd2.Text = datareader("project_finish").ToString
lblProjectBudget.Text = datareader("project_budget").ToString
lblProjectBudget2.Text = datareader("project_budget").ToString
lblProjectCost.Text = datareader("project_cost").ToString
lblProjectCost2.Text = datareader("project_cost").ToString
' lblProjectLeader.Text = datareader("project_cost").ToString
'lblProjectExpenditures.Text = agdgssag
Dim StartDate As DateTime = datareader("project_start")
Dim FinishDate As DateTime = datareader("project_finish")
Dim today As DateTime = DateTime.Now
Dim sumDays = (FinishDate - StartDate).TotalDays
Dim daysToNow = (today - StartDate).TotalDays
Dim percentage = daysToNow / sumDays * 100
Dim percentageLeft = 100 - percentage
Session("PercentageCompleted") = percentage
Session("PercentageLeft") = percentageLeft
GetTable()
Expenditures()
lblProjectPercentage.Text = percentage.ToString("N2") + "%"
End While
datareader.Close()
conn.Close()
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Private Sub BindData()
Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
Dim query As New SqlCommand("SELECT Items.item_name, Items.item_cost, project_items.item_quantity FROM Items INNER JOIN project_items ON items.item_id = project_items.item_id WHERE project_items.project_id = #parameter", conn)
conn.Open()
query.Parameters.AddWithValue("#parameter", Convert.ToInt32(Session("ProjectID")))
Dim da As New SqlDataAdapter(query)
da.SelectCommand = query
Dim table As New DataTable()
da.Fill(table)
grdItems.DataSource = table
conn.Close()
grdItems.DataBind()
End Sub
Protected Sub grdItems_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
myTotal += (CDec(rowView("item_cost")) * CDec(rowView("item_quantity")))
End If
If e.Row.RowType = DataControlRowType.Footer Then
Dim lblTotalPrice As Label = DirectCast(e.Row.FindControl("lblTotalPrice"), Label)
lblTotalPrice.Text = myTotal.ToString()
End If
End Sub
Function GetTable() As DataTable
Dim table As New DataTable
table.Columns.Add("Percentage Completed", GetType(Double))
table.Columns.Add("Percentage Not-Completed", GetType(Double))
table.Rows.Add(Session("PercentageCompleted"))
table.Rows.Add(Session("PercentageLeft"))
Chart1.DataSource = table
Chart1.Series("Series1").XValueMember = "Percentage Not-Completed"
Chart1.Series("Series1").YValueMembers = "Percentage Completed"
Chart1.DataBind()
Return table
End Function
Private Sub Expenditures()
Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
Dim projExpComm As String = "SELECT Items.item_cost, project_items.item_quantity FROM Items INNER JOIN project_items ON items.item_id = project_items.item_id WHERE project_items.project_id = #parameter"
Dim projExpSQL As New SqlCommand
conn.Open()
projExpSQL = New SqlCommand(projExpComm, conn)
projExpSQL.Parameters.AddWithValue("#parameter", Session("project_id"))
Dim datareader As SqlDataReader = projExpSQL.ExecuteReader()
datareader.Read()
While datareader.Read
While datareader.HasRows
Dim ItemsTotal As Double = 0
For Each row In datareader
Dim ItemCost = datareader("item_cost")
Dim ItemQuantity = datareader("item_quantity")
Dim ItemsSubTotal As Double = ItemCost * ItemQuantity
ItemsTotal = ItemsSubTotal
Next
ItemsTotal = ItemsTotal + ItemsTotal
lblProjectExpenditures.Text = ItemsTotal.ToString
End While
End While
datareader.Close()
conn.Close()
End Sub
End Class
Why is it happening?
I checked for un-closed connections / datareaders but everything was ok.
Is there something I'm missing ?
You page load method is executed completely each time you do a post back. You should be checking to see !isPostBack to prevent the complete execution of that code.