You are here:

ASP.NET/help with asp.net and sql statement

Advertisement


Question
Hi  
I have a table with four columns a,b, c,d  and a form with two text boxes TextBox1 and TextBox2.I want to multiply column a with textbox1 , column b with text box2 and output in column c and column d. any help would be appreciated

Imports System.Data
Imports System.Data.OleDb
Public Class WebForm1
   Inherits System.Web.UI.Page

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   End Sub

   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
       Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ab.accdb;Jet OLEDB:Database Password=***")
       Dim CmdStr As String = "insert " + TextBox1.Text + " as a, " + TextBox2.Text + " as b, a * b AS c FROM Table1"
       con.Open()
       Dim cmd As OleDbCommand = New OleDbCommand(CmdStr, con)
       cmd.ExecuteNonQuery()
       con.Close()
       MsgBox("Done")


   End Sub
End Class

Answer
I am not sure what u are trying to do. But from you code seems that u are just learning .net.

I have modified the code and hope it should work.
I assume there is always one row in you MS Access table

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ab.accdb;Jet OLEDB:Database Password=***")

       Dim da As New OleDbDataAdapter
       Dim cmd As New OleDbCommand
       Dim dt As New DataTable
       Dim qry As String

       qry = "Select top 1 a,b from Table1"
       cmd.CommandText = qry
       cmd.CommandType = CommandType.Text
       cmd.Connection = con
       da.SelectCommand = cmd

       con.Open()
       da.Fill(dt)
       con.Close()

       Dim a As Integer
       Dim b As Integer
       Dim c As Integer
       Dim d As Integer

       a = Convert.ToInt32(dt(0)(0))
       b = Convert.ToInt32(dt(0)(1))
       c = TextBox1.Text * a
       d = TextBox2.Text * b

       Dim CmdStr As String = "insert into table1 values (" & a & "," & b & "," & c & "," & d & ")"
       con.Open()
       Dim cmd1 As OleDbCommand = New OleDbCommand(CmdStr, con)
       cmd1.ExecuteNonQuery()
       con.Close()
       MsgBox("Done")


It there are any errors please try to resolve them yourself..It will be beneficial for you

ASP.NET

All Answers


Answers by Expert:


Ask Experts

Volunteer


Aman Bhullar

Expertise

1. Asp.Net(C#, VB) 2. IIS(6.0) 3. C# 4. Classic ASP 5. JavaScript

Experience

I have nearly 7 Years of Experience in Field of IT with Asp.net Developer (Portal, Dynamic websites ,CRM's, CMS) Visual studio.net 2003,2005 (VB.net , C#.net) Database (SQL server 2005), Stored procedures IIS 5-6 JavaScript, CSS, DHTML, XML

Education/Credentials
Hold Masters in Computer Applications

Awards and Honors
Contributor Member of Asp.net Achieved Master Certification(Programming for ASP.NET) [Experts-exchange.com]

©2016 About.com. All rights reserved.