when I click on the register button I get the test "registration successful" even if I didn't enter anything on the column.
I need to display "registration successful" if it successfully inserted to the database.
if it is a failure I need it to display "registration fail" on the same label (lblsuccessfail)
Imports System.Data.SqlClient
Imports System.IO
Partial Class register
Inherits System.Web.UI.Page
Dim cn As New SqlConnection("Data Source=localhost\MSSQL2012;Initial Catalog=UserDetails;Integrated Security=True")
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnnew.Click
cn.Open()
Try
Dim Query As String
Query = "INSERT INTO FirstProject (fullname,username,password,address,gender)VALUES('" & txtfullname.Text & "', '" & txtusername.Text & "','" & txtpassword.Text & "','" & txtaddress.Text & "','" & cbogender.Text & "')"
cmd = New SqlCommand(Query, cn)
If cmd.ExecuteNonQuery() Then
lblsuccessfail.Text = lblsuccessfail.Text & " Registertration successful "
End If
Catch ex As Exception
lblsuccessfail.Text = lblsuccessfail.Text & " Registertration unsuccessful "
End Try
cn.Close()
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnback_Click(sender As Object, e As System.EventArgs) Handles btnback.Click
Response.Redirect("login.aspx")
End Sub
End Class
Imports System.Data.SqlClient
Imports System.IO
Partial Class register
Inherits System.Web.UI.Page
Dim cn As New SqlConnection("Data Source=localhost\MSSQL2012;Initial Catalog=UserDetails;Integrated Security=True")
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnnew.Click
Try
cn.Open()
cmd = New SqlCommand("INSERT INTO FirstProject (fullname,username,password,address,gender)VALUES('" & txtfullname.Text & "', '" & txtusername.Text & "','" & txtpassword.Text & "','" & txtaddress.Text & "','" & cbogender.Text & "')", cn)
If txtfullname.Text = Nothing OrElse txtusername.Text IsNot Nothing Or txtpassword.Text.Equals(Nothing) OrElse txtaddress.Text = String.Empty Or cbogender.Text.Equals(String.Empty) Then Throw New Exception()
If cmd.ExecuteNonQuery() Then lblsuccessfail.Text = lblsuccessfail.Text & " Registertration successful "
Catch ex As Exception
lblsuccessfail.Text = lblsuccessfail.Text & " Registertration unsuccessful "
Finally
cn.Close()
End Try
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnback_Click(sender As Object, e As System.EventArgs) Handles btnback.Click
Response.Redirect("login.aspx")
End Sub
End Class
Related
please code solution and for rename row header I mean marking the color of the image file I attached
Note : for notes I use visual studio 2010
Public Class Form1
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
dbConnection()
Dim indexID As Integer
x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
indexID = Convert.ToInt32((DataGridView1.Rows(x).Cells(0).Value.ToString()))
sql = "UPDATE ITEM SET ITM= '" & txtFname.Text & "',ITC='" & txtLname.Text & "',QOH='" & cbGender.Text & "' WHERE ID=" & indexID & ""
cmd = New OleDbCommand(sql, conn)
cmd.ExecuteNonQuery()
MessageBox.Show("Successfully Updated...", "Update")
conn.Close()
'btnReadDatabase_Click(sender, e);
retrieveALL()
End Sub
End Class
object reference not set
Thanks
roy
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
dbConnection()
sql = "UPDATE ITEM SET ITM= '" & txtFname.Text & "',ITC='" & txtLname.Text & "',QOH='" & cbGender.Text & "' WHERE ITC='" & txtLname.Text & "'"
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
MessageBox.Show("Successfully Updated...", "Update")
con.Close()
PopulateDataGridView()
End Sub
Code :
Private m_cn As New SqlConnection
Private m_DA As SqlDataAdapter
Private m_CB As SqlCommandBuilder
Private m_DataTable As New DataTable
Private m_intRowPosition As Integer = 0
Private Sub InsertDatabaseItem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
m_cn.ConnectionString = "Data Source=My-PC\SQLSERVEREXPRESS;Initial Catalog=ConvienienceProducts;Integrated Security=True"
m_cn.Open()
m_DA = New SqlDataAdapter("Select * From ProductIndex", m_cn)
m_CB = New SqlCommandBuilder(m_DA)
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Dim cmd As New SqlCommand(("INSERT INTO ProductIndex VALUES(" &
txtID.Text & "," &
txtName.Text & "," &
txtPrice.Text & "," &
txtDesc.Text & ")"), m_cn)
cmd.ExecuteNonQuery()
MsgBox("Success....", MsgBoxStyle.Information, "SUCCESS")
Me.Hide()
txtID.Clear()
txtName.Clear()
txtPrice.Clear()
txtDesc.Clear()
m_cn.Close()
m_cn.Dispose()
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Hide()
End Sub
This is the error message :
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near ','.
You code should be using parameters. Try this:
Dim cmd As New SqlCommand(("INSERT INTO ProductIndex VALUES(" &
"#ID," &
"#Name," &
"#Price," &
"#Desc)"), m_cn)
cmd.Parameters.Add("#ID", SqlDbType.Char)
cmd.Parameters("#ID").Value = txtID.Text
cmd.Parameters.Add("#Name", SqlDbType.Char)
cmd.Parameters("#Name").Value = txtName.Text
cmd.Parameters.Add("#Price", SqlDbType.Char)
cmd.Parameters("#Price").Value = txtPrice.Text
cmd.Parameters.Add("#Desc", SqlDbType.Char)
cmd.Parameters("#Desc").Value = txtDesc.Text
The types are probably wrong (especially Price, and probably ID), but as you know what they are, and I don't, you can easily correct them.
You need to wrap your string values in single quotes
Dim cmd As New SqlCommand(("INSERT INTO ProductIndex VALUES('" & txtID.Text & "',
Hard to see but there is a single quote just before and after the double quotes surrounding your variables.
I have answered your particular question but you should be using parameters
Hi guys: Can any one please help me out in this error? Thanks
Error 1 Operator '&' is not defined for types 'String' and 'System.Windows.Forms.TextBox'. C:\Users\kathy\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 20 20 WindowsApplication1
Imports System.Data.OleDb
Public Class Form1
Public connection As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\kathy\Desktop\generalledger.accdb"
Public conn As New OleDbConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn.ConnectionString = connection
If conn.State = ConnectionState.Closed Then
conn.Open()
MsgBox("Open")
Else
MsgBox("Closed")
End If
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
Dim sqlQuery As String
sqlQuery = "insert into voucher(VoucherName, Account_Code, Amount) values ('" & TxtVoucher & "', " & TxtAmount & ", '" & TxtAccount & "')"
Dim sqlcommand As New OleDbCommand
With sqlcommand
.CommandText = sqlQuery
.Connection = conn
.ExecuteNonQuery()
End With
MsgBox("Save")
End Sub
End Class
Just add .Text() in your textboxes.
sqlQuery = "insert into voucher(VoucherName, Account_Code, Amount) values ('" & TxtVoucher & "', " & TxtAmount & ", '" & TxtAccount & "')"
To
sqlQuery = "insert into voucher(VoucherName, Account_Code, Amount) values ('" & TxtVoucher.Text() & "', " & TxtAmount.Text() & ", '" & TxtAccount.Text() & "')"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn.ConnectionString = connection
If conn.State = ConnectionState.Closed Then
conn.Open()
MsgBox("Open")
Else
MsgBox("Closed")
End If
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
conn.Open()
Dim sqlQuery As String
sqlQuery = "insert into voucher(VoucherName, Account_Code, Amount) values ('"+ TxtVoucher + "', " + TxtAmount + ", '" + TxtAccount + "')"
Dim sqlcommand As New OleDbCommand
With sqlcommand
.CommandText = sqlQuery
.Connection = conn
.ExecuteNonQuery()
conn.Close()
End With
MsgBox("Save")
End Sub
I have created a database in MS Access 2007 and a front end in Visual Studio 2010. The code is as follows:
Imports System.Data.OleDb
Public Class Form3
Public connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Course_Data.accdb;persist security info=false"
Dim cn As New OleDbConnection
'Dim cn As New OleDbConnection(My.Settings.Course_DataConnectionString)
Private Sub Label14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label14.Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim sqlquery As String = "insert into Participant_info values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox11.Text & "','" & TextBox12.Text & "','" & TextBox13.Text & "','" & TextBox14.Text & "','" & TextBox15.Text & "', '" & TextBox16.Text & "')"
Dim cmd As New OleDbCommand
With cmd
.CommandText = sqlquery
.Connection = cn
.ExecuteNonQuery()
End With
MsgBox("Details Inserted")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.ConnectionString = connstring
If cn.State = ConnectionState.Closed Then
MsgBox("open")
Else
MsgBox("close")
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
cn.ConnectionString = connstring
cn.Open()
TextBox17.Text = ("Connected")
End Sub
End Class
It shows a message as data being inserted, but the data is not there in the database.
Sometimes Data Source=|DataDirectory|\... is problematic when debugging. Please bear on mind that you'll have another database in \bin\debug at your project folder when you are debugging your code. Probably you're updating the records in this database instead the original one.
Try to set an absolute path and check if the records are being updated.
Also, your code is vulnerable to SQL Injection. You should build your OleDbCommand as follows:
Dim cmd As New OleDbCommand("insert into Participant_info values('?','?','?',······)", cn)
cmd.Parameters.Add(TextBox1.Text)
cmd.Parameters.Add(TextBox2.Text)
cmd.Parameters.Add(TextBox3.Text)
...
...
cmd.ExecuteNonQuery()
cn.close()
I want to know how to take a backup of database using programatically in VB.net of SQL Server 2005. If you have an idea about it then please send me the code for that.
SQL Server Database Backup Utility using VB.NET and SQL-DMO (New version)
You can achieve this by using Sql Management Object. SQL Server Management Objects (SMO) are objects designed for programmatic management of Microsoft SQL Server.
Refer How to: Back Up Databases and Transaction Logs in Visual Basic .NET
Imports System.IO
Imports System.Data.SqlClient
Public Class Form2
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim dread As SqlDataReader
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
server(".")
server(".\sqlexpress")
End Sub
Sub server(ByVal str As String)
con = New SqlConnection("Data Source=" & str & ";Database=Master;integrated security=SSPI;")
con.Open()
cmd = New SqlCommand("select * from sysservers where srvproduct='SQL Server'", con)
dread = cmd.ExecuteReader
While dread.Read
ComboBox1.Items.Add(dread(2))
End While
dread.Close()
con.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ComboBox1.Text = "" Then
MsgBox("select the server name")
ComboBox1.Focus()
Exit Sub
End If
Try
ComboBox2.Text = ""
con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;")
con.Open()
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
ComboBox2.Text = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)
cmd = New SqlCommand("drop database " & ComboBox2.Text & " ", con)
cmd.ExecuteNonQuery()
cmd = New SqlCommand("RESTORE DATABASE " & ComboBox2.Text & " FROM disk='" & OpenFileDialog1.FileName & "'", con)
cmd.ExecuteNonQuery()
MsgBox("Restore Successfully Completed")
ComboBox1.Text = ""
ComboBox2.Text = ""
Else
MsgBox("Restore Cancelled")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox1.Text = "" Then
MsgBox("select the server name")
ComboBox1.Focus()
Exit Sub
ElseIf ComboBox2.Text = "" Then
MsgBox("select the database name")
ComboBox2.Focus()
Exit Sub
End If
Try
con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;")
con.Open()
SaveFileDialog1.FileName = ComboBox2.Text + ".bak"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim s As String
s = SaveFileDialog1.FileName
cmd = New SqlCommand("backup database " & ComboBox2.Text & " to disk='" & s & "'", con)
cmd.ExecuteNonQuery()
MsgBox("Back Up Successfully Created")
ComboBox1.Text = ""
ComboBox2.Text = ""
con.Close()
Else
MsgBox("Back Up Cancelled")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;")
con.Open()
ComboBox2.Items.Clear()
cmd = New SqlCommand("select * from sysdatabases", con)
dread = cmd.ExecuteReader
While dread.Read
ComboBox2.Items.Add(dread(0))
End While
dread.Close()
con.Close()
End Sub
End Class