How to rebind grid view after update? - asp.net

May I know how to rebind data to grid view table after update? Below is what I have tried so far:
Dim conn As New MySqlConnection
conn.ConnectionString = "server = localhost; user id = root; password = root; database = db_fyp"
Dim com As New MySqlCommand
Dim dt As New DataTable
Dim query As String
query = "update tblorder set OrderStatus = #OrderStatus where UserId = #UserId "
com = New MySqlCommand(query, conn)
com.Parameters.AddWithValue("#OrderStatus", tOrderStatus)
com.Parameters.AddWithValue("#UserId", Session("Username"))
conn.Open()
com.ExecuteNonQuery()
BindData()
conn.Close()
Here is my BindData function for insert data into grid view table. This part is working perfectly.
Private Sub BindData()
Dim conn As New MySqlConnection
conn.ConnectionString = "server = localhost; user id = root; password = root; database = db_fyp"
Dim com As New MySqlCommand
Dim dr As MySqlDataReader
Dim query As String
Dim dt As New DataTable
conn.Open()
query = "select FoodName, Qty, IngredientName, Quantity, OrderStatus from tblorder, tblorderdetail, tblfood, tblcustomizefooddetail, tblcustomizeingredient, tblordercustomize where UserId = #UserId and OrderStatus = #OrderStatus and tblorder.OrderId = tblorderdetail.OrderId and tblorderdetail.FoodId = tblfood.FoodId and tblorderdetail.OrderDetailId = tblordercustomize.OrderDetailId and tblfood.FoodId = tblcustomizefooddetail.FoodId and tblcustomizeingredient.IngredientId = tblcustomizefooddetail.IngredientId and tblordercustomize.IngredientId = tblcustomizeingredient.IngredientId"
com = New MySqlCommand(query, conn)
com.Parameters.AddWithValue("#UserId", Session("Username"))
com.Parameters.AddWithValue("#OrderStatus", "Pending")
dr = com.ExecuteReader
dt.Load(dr)
Session("OderTable") = dt
GridView1.DataSource = dt
GridView1.DataBind()
conn.Close()
End Sub

you already have a function to populate the grid, simply call it from within the updating code:
Dim conn As New MySqlConnection
conn.ConnectionString = "server = localhost; user id = root; password = root; database = db_fyp"
Dim com As New MySqlCommand
Dim dt As New DataTable
Dim query As String
query = "update tblorder set OrderStatus = #OrderStatus where UserId = #UserId "
com = New MySqlCommand(query, conn)
com.Parameters.AddWithValue("#OrderStatus", tOrderStatus)
com.Parameters.AddWithValue("#UserId", Session("Username"))
conn.Open()
com.ExecuteNonQuery()
conn.Close()
'you dont need all that
'dt.Load(????)
'GridView1.DataSource = dt
'GridView1.DataBind()
'just call this
BindData()

Related

I got an error at da.Fill(ds) command for my shopping cart project

This is my final-year mini-project. I tried to implement a shopping cart project.
But I get an error in filling details on gridview. I tried the below coding:
If Not IsPostBack Then
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add("sno")
dt.Columns.Add("productid")
dt.Columns.Add("productname")
dt.Columns.Add("price")
dt.Columns.Add("productimage")
dt.Columns.Add("cost")
dt.Columns.Add("totalcost")
If Request.QueryString("id") IsNot Nothing Then
If Session("Buyitems") Is Nothing Then
dr = dt.NewRow()
Dim mycon As String = "Data Source=Sandy-PC\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"
Dim scon As SqlConnection = New SqlConnection(mycon)
Dim myquery As String = "select * from decorativestands where Id=" & Request.QueryString("id")
Dim cmd As SqlCommand = New SqlCommand()
cmd.CommandText = myquery
cmd.Connection = scon
Dim da As SqlDataAdapter = New SqlDataAdapter()
da.SelectCommand = cmd
Dim ds As DataSet = New DataSet()
da.Fill(ds)
dr("sno") = 1
dr("productid") = ds.Tables(0).Rows(0)("productid").ToString()
dr("productname") = ds.Tables(0).Rows(0)("productname").ToString()
dr("productimage") = ds.Tables(0).Rows(0)("productimage").ToString()
dr("price") = ds.Tables(0).Rows(0)("price").ToString()
dt.Rows.Add(dr)
GridView1.DataSource = dt
GridView1.DataBind()
Session("buyitems") = dt
Else
dt = CType(Session("buyitems"), DataTable)
Dim sr As Integer
sr = dt.Rows.Count
dr = dt.NewRow()
I got "sqlexception was unhandled by user code" error.

Search record in grid view

The read record is solved. I still have another problem the error message still remain there after user key in correct order id.
query = "select FoodName, Qty, IngredientName, Quantity, OrderStatus
from tblorder, tblorderdetail, tblfood, tblcustomizefooddetail, tblcustomizeingredient, tblordercustomize
where tblOrder.OrderId = #OrderId and OrderStatus = #OrderStatus and tblorder.OrderId = tblorderdetail.OrderId and tblorderdetail.FoodId = tblfood.FoodId and tblorderdetail.OrderDetailId = tblordercustomize.OrderDetailId and tblfood.FoodId = tblcustomizefooddetail.FoodId and tblcustomizeingredient.IngredientId = tblcustomizefooddetail.IngredientId and tblordercustomize.IngredientId = tblcustomizeingredient.IngredientId"
com = New MySqlCommand(query, conn)
com.Parameters.AddWithValue("#OrderId", txtOrderId.Text)
com.Parameters.AddWithValue("#OrderStatus", "Pending")
Dim sqladapter As New MySqlDataAdapter(com)
sqladapter.Fill(dt)
If dt.Rows.Count Then
GridView1.DataSource = dt
GridView1.DataBind()
Else
Label1.Text = "Order ID Doesn't Exist"
End If
Lets take your sample and modify it:
query = "select FoodName, Qty, IngredientName, Quantity, OrderStatus
from tblorder, tblorderdetail, tblfood, tblcustomizefooddetail, tblcustomizeingredient, tblordercustomize
where tblOrder.OrderId = #OrderId and OrderStatus = #OrderStatus and tblorder.OrderId = tblorderdetail.OrderId and tblorderdetail.FoodId = tblfood.FoodId and tblorderdetail.OrderDetailId = tblordercustomize.OrderDetailId and tblfood.FoodId = tblcustomizefooddetail.FoodId and tblcustomizeingredient.IngredientId = tblcustomizefooddetail.IngredientId and tblordercustomize.IngredientId = tblcustomizeingredient.IngredientId"
com = New MySqlCommand(query, conn)
com.Parameters.AddWithValue("#OrderId", txtOrderId.Text)
com.Parameters.AddWithValue("#OrderStatus", "Pending")
Dim adapter as new SqlDataAdapter(com)
adapter.Fill(dt)
if adapter.Tables(0).Rows.Count > 0
GridView1.DataSource = adapter.Tables(0)
GridView1.DataBind()
Else
Label1.Text = "Order ID Doesn't Exist"
End If

The thread was interrupted

I am using this code to update database with new values. But it return message The thread was interrupted. What does it mean? What is wrong with my code?
Dim connStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("DeskriptivaConnectionString").ConnectionString.ToString()
Dim txtName As Object = DirectCast(FormView1.FindControl("txtName"), TextBox)
Dim txtLastName As Object = DirectCast(FormView1.FindControl("txtLastName"), TextBox)
Dim txtInfo As Object = DirectCast(FormView1.FindControl("txtInfo"), TextBox)
Dim txtCity As Object = DirectCast(FormView1.FindControl("txtCity"), TextBox)
Dim txtPrize As TextBox = DirectCast(FormView1.FindControl("txtPrize"), TextBox)
Dim txtPhone As TextBox = DirectCast(FormView1.FindControl("txtPhone"), TextBox)
Dim txtMail As TextBox = DirectCast(FormView1.FindControl("txtMail"), TextBox)
Try
Using conn As New SqlConnection(connStr)
Dim cmd As SqlCommand = conn.CreateCommand()
cmd.CommandText = "UPDATE Profiles SET #Name = Name, #LastName = LastName, #Info = Info, #City = City, #Prize = Prize, #Phone = Phone, #Mail = Mail WHERE (UserName = #UserName)"
cmd.Parameters.Add("#Name", System.Data.SqlDbType.NVarChar).Value = txtName.Text
cmd.Parameters.Add("#LastName", System.Data.SqlDbType.NVarChar).Value = txtLastName.Text
cmd.Parameters.Add("#Info", System.Data.SqlDbType.NText).Value = MakeLink(HtmlRemoval.StripTagsCharArray(txtInfo.Text))
cmd.Parameters.Add("#City", System.Data.SqlDbType.NVarChar).Value = txtCity.Text
cmd.Parameters.Add("#Prize", System.Data.SqlDbType.NVarChar).Value = txtPrize.Text
cmd.Parameters.Add("#Phone", System.Data.SqlDbType.NVarChar).Value = txtPhone.Text
cmd.Parameters.Add("#Mail", System.Data.SqlDbType.NVarChar).Value = txtMail.Text
cmd.Parameters.Add("#UserName", System.Data.SqlDbType.NVarChar).Value = Context.User.Identity.Name
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
Response.Redirect(ResolveClientUrl("~/Profil/"))
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
Use Response.Redirect(ResolveClientUrl("~/Profil/"), false) with Context.ApplicationInstance.CompleteRequest(); instead of Response.Redirect(ResolveClientUrl("~/Profil/"))
check Correct use of System.Web.HttpResponse.Redirect blog post for more information
and also your sql parameters # symbol is in wrong place. change as below
UPDATE Profiles SET
Name = #Name, LastName
= #LastName, Info =
#Info, City = #City,
Prize = #Prize, Phone
= #Phone, Mail = #Mail
WHERE (UserName =
#UserName)

Must declare the scalar variable "#user_code" in parameterized query

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ds As DataSet = New DataSet()
Dim param As SqlParameter = New SqlParameter("#user_code", SqlDbType.Char, 4)
param.Value = "0016"
Const sqlstr As String = "select sectors.sector_code,sector_name from user_sectors inner join sectors on user_sectors.sector_code = sectors.sector_code where user_code = #user_code and sectors.sector_code not in ('z')"
Dim da As SqlDataAdapter = New SqlDataAdapter(sqlstr, _con)
da.Fill(ds)
ddl.DataValueField = "sector_code"
ddl.DataTextField = "sector_name"
ddl.DataSource = ds.Tables(0)
ddl.DataBind()
End Sub
Code for adding parameter to dataadapter
SqlConnection conn = new SqlConnection("Data
Source=localhost;Database=Northwind;Integrated Security=SSPI");
SqlCommand command = new SqlCommand("GetProducts", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("#CategoryID", SqlDbType.Int).Value = 1;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds, "Products");
add parameter to you data adapter object like this
da.Parameters.Add(param)
this is missing in your code
MSDN : Using Parameters with a DataAdapter

output parameter into label

instead of returning my output paremeter value in my stored procedure to my label it returns the default value i set my output parameter to.
why cant i put my output parameter into my text label
Dim reader As SqlDataReader
cmd.Parameters.AddWithValue("#tour", "2365")
cmd.Parameters.Add("#tourname", SqlDbType.VarChar)
cmd.Parameters("#tourname").Direction = ParameterDirection.Output
cmd.CommandText = "test"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
conn.Open()
reader = cmd.ExecuteReader()
Dim myTable As DataTable = New DataTable()
myTable.Load(reader)
DropDownList1.DataSource = myTable
DropDownList1.DataTextField = "ddate7"
DropDownList1.DataBind()
Label1.Text = cmd.Parameters("#tourname").ToString
conn.Close()
You haven't defined the #tourname parameter as an output parameter.
Dim param as New SqlParameter("tourname", 2356)
param.Direction = ParameterDirection.Output
cmd.Parameters.Add(param)

Resources