I have two textboxes, one is for the ProductName and the 2nd is for ProductPrice.
I am not able to get the txtProductPrice value from the database while entering the txtProductname. Please help me
Here is the code I am trying to execute...
Try
conn.Open()
cmd.Connection = conn
cmd.CommandText = "SELECT price FROM products WHERE name = '" & txtPN.Text & "'"
dr = cmd.ExecuteReader
If dr.Read Then
'what will i do here???
End If
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Related
Try
con.Open()
dt2 = New DataTable
With cmd
.Connection = con
.CommandText = "Select Lname,Fname,Mname,Gender,Grade,Saint,SY from g7 where saint='" & cinfosection.Text & " and Lname like '%" & TextBox1.Text & "%''
End With
da.SelectCommand = cmd
da.Fill(dt2)
dgclassinfo.DataSource = dt2
Catch ex As Exception
MsgBox(ex.Message)
End Try
da.Dispose()
con.Close()
I'm using the code above to search data in dgv wherein the result changes everytime the user inputs per character (textbox.Textchanged). The problem is I want the user to search using the data in combobox(dropdownlist). everytime the user select different value from the combobox another result is displayed in dgv INSTANTLY
Anyhelp is appreciated
I have multiple queries need to be shown on single Datagridview. Query runs one at a time. The problem is when while running the second query the previous query data columns will still exist and concatenate to new query.
Example Query1: A,B,C,D columns are shown on datagridview
Query2: has E,F,G columns but when I run the Query 2 it shows A,B,C,D,E,F,G.
I need only E,F,G to be displayed. wasnt able to clear the columns
Query1:
With cmd
.Connection = con
.CommandText = "SELECT vouchertype as [Voucher Type], voucherdate as [Voucher Date],VoucherNo as [Voucher No.], Party as [party Name], DISCOUNT,TAX,NETAMOUNT FROM SALESREPORT where vouchertype='Sale' and VOUCHERDATE BETWEEN '" & DATE1.Text & "' AND '" & DATE2.Text & "' "
End With
da.SelectCommand = cmd
dt.Clear()
da.Fill(dt)
DataGridView1.DataSource = dt
con.Close()
Query2:
With cmd
.Connection = con
.CommandText = "SELECT DESCRIPTION AS [ITEM PARTICULAR], [TYPE] AS [TRANSACTION], REF AS [REFERENCE], QTY AS [QUANTITY], OPENINGBALANCE, OPENINGVALUE, INWARDSQTY,INWARDSVALUE, OUTWARDSQTY,OUTWARDSVALUE FROM STOCK_MOVEMENT WHERE DATES BETWEEN '" & DATE1.Text & "' AND '" & DATE2.Text & "' "
End With
da.SelectCommand = cmd
dt.Clear()
da.Fill(dt)
DataGridView1.DataSource = dt
con.Close()
Things I Tried:
Try
dt.Columns.Clear()
dt.Rows.Clear()
DataGridView1.DataSource = dt
Catch ex As Exception
End Try
Try
DataGridView1.Rows.Clear()
Catch ex As Exception
End Try
Try
dt.Clear()
Catch ex As Exception
End Try
Try
dTable.Clear()
Catch ex As Exception
End Try
Try
DataGridView1.Columns.Clear()
Catch ex As Exception
End Try
Try
DataGridView1.DataSource = Nothing
Catch ex As Exception
End Try
Try
dt.Clear()
da.Fill(dt)
Catch ex As Exception
End Try
Try
DataGridView1.Rows.Clear()
DataGridView1.Refresh()
Catch ex As Exception
End Try
Try
DataGridView1.DataSource = Nothing
Catch ex As Exception
End Try
Try
ds.Clear()
DataGridView1.Rows.Clear()
Catch ex As Exception
End Try
Create a new DataTable each time rather than reusing an existing one.
DataGridView1.DataSource = Nothing
DataGridView1.Columns.Clear()
DataGridView1.DataSource = dt
I use below code but it gives error on sentence icount = cmd.ExecuteNonQuery
cn.Open()
str = "SELECT [srno],[caste]FROM [SchoolERP].[dbo].[caste] where (caste ='" + (TextBox1.Text) + "')"
cmd = New SqlCommand(str, cn)
dr1 = cmd.ExecuteReader()
If Not dr1.HasRows Then
str = "INSERT INTO [SchoolERP].[dbo].[caste]([caste])VALUES('" + TextBox1.Text + "')"
cmd = New SqlCommand(str, cn)
icount = cmd.ExecuteNonQuery
MessageBox.Show(icount)
Else
MsgBox("Record Exists")
cn.Dispose()
End If
cn.Close()
Try always calling the Close method when you have finished using the DataReader object.
dr1.Close();
Another optionis to turn on MARS , in your connection string just add "MultipleActiveResultSets=True;"
Try this:
Try
insert data in combobox
cmd.Connection = con
cmd.CommandText = "select name from party"
dr = cmd.ExecuteReader()
cb_ms.Items.Add("---Select---")
cb_ms.SelectedIndex = 0
While dr.Read() 'get error here.'
cb_ms.Items.Add(dr("name"))
End While
dr.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
You could just close the datareader before the 2nd query, but it's better still to get this all into a single query in the first place. Moreover, you really need to close that awful sql injection issue.
Try this to fix both issues:
Dim sql As String = _
"IF NOT EXISTS
(
SELECT 1 FROM [SchoolERP].[dbo].[caste] where (caste = #caste )
)
BEGIN
INSERT INTO [SchoolERP].[dbo].[caste]([caste])VALUES( #caste)
END"
Using cn As New SqlConnection("connection string here"), _
cmd As New SqlCommand(sql, cn)
'Use actual columnn type from the database here
cmd.Parameters.Add("#caste", SqlDbType.NVarChar, 50).Value = TextBox1.Text
cn.Open()
icount = cmd.ExecuteNonQuery()
MessageBox.Show(icount)
End Using
Use Try.. Catch.. Finally... End Try somehing like this:
Try
cn.Open()
str = "SELECT [srno],[caste]FROM [SchoolERP].[dbo].[caste] where (caste ='" + (TextBox1.Text) + "')"
cmd = New SqlCommand(str, cn)
dr1 = cmd.ExecuteReader()
If Not dr1.HasRows Then
str = "INSERT INTO [SchoolERP].[dbo].[caste]([caste])VALUES('" + TextBox1.Text + "')"
cmd = New SqlCommand(str, cn)
icount = cmd.ExecuteNonQuery
MessageBox.Show(icount)
Else
MsgBox("Record Exists")
cn.Dispose()
End If
cn.Close()
Catch error As Exception
........
Finally
dr1.Close
End Try
I got Answer ... i only close connection before icount = cmd.ExecuteNonQuery this sentence and again open connection...and its works..
thanks disha..
Please help me... i am trying to update my database from VB.net. It shows error. My code is given below....
Try
getConnect()
Dim strSQL As String
strSQL = " UPDATE DEPARTMENT SET [DEP_ID]=#DEP_ID,[DEPART]=#DEPART, [DEP_DSCRPTN]=#DEP_DSCRPTN WHERE [DEP_ID] = #DEP_ID"
Dim cmd As New SqlCommand(strSQL, Conn)
cmd.Parameters.AddWithValue("#DEP_ID", CInt(Me.DEPID.Text))
cmd.Parameters.AddWithValue("#DEPART", SqlDbType.VarChar).Value = CMBDEPT.Text
cmd.Parameters.AddWithValue("#DEP_DSCRPTN", SqlDbType.VarChar).Value = TXTDESC.Text
Conn.Open()
cmd.ExecuteNonQuery()
MsgBox("Update Complete!", MsgBoxStyle.Information, "Update")
Catch ex As Exception
MsgBox("ERROR: " + ex.Message, MsgBoxStyle.Information, "Update")
Finally
Conn.Close()
BTNCLEAR.PerformClick()
End Try
And the error is:
ERROR: Cannot update identity column 'DEP_ID'
Remove [DEP_ID]=#DEP_ID, from the SET. It makes no sense to try and set it to the value already ensured by the WHERE anyway so it is clearly redundant and it is not permitted to update IDENTITY columns.
UPDATE DEPARTMENT
SET [DEPART] = #DEPART,
[DEP_DSCRPTN] = #DEP_DSCRPTN
WHERE [DEP_ID] = #DEP_ID
The error message is clear, remove the Set DEP_ID = #DEP_ID part.
Try
getConnect()
Dim strSQL As String
strSQL = "UPDATE DEPARTMENT SET [DEPART]=#DEPART," +
"[DEP_DSCRPTN]=#DEP_DSCRPTN WHERE [DEP_ID] = #DEP_ID"
Dim cmd As New SqlCommand(strSQL, Conn)
cmd.Parameters.AddWithValue("#DEP_ID", CInt(Me.DEPID.Text))
cmd.Parameters.AddWithValue("#DEPART", SqlDbType.VarChar).Value = CMBDEPT.Text
cmd.Parameters.AddWithValue("#DEP_DSCRPTN", SqlDbType.VarChar).Value = TXTDESC.Text
Conn.Open()
cmd.ExecuteNonQuery()
MsgBox("Update Complete!", MsgBoxStyle.Information, "Update")
Catch ex As Exception
MsgBox("ERROR: " + ex.Message, MsgBoxStyle.Information, "Update")
Finally
Conn.Close()
BTNCLEAR.PerformClick()
End Try
If you find yourself in need to change an Identity column then I think you need to analyze better your database schema.
I am trying to do an update query from a winform with two variables without using a dataset.
I assign both of my variable and then run the query but it keeps giving the error that zcomp is not a valid column name. Which of course is true but I tell it which column before I say = zcomp. Below is my code that is running the query.
Dim zamnt As Integer = WopartsDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
Dim zcomp As Integer = gridRow.Cells(0).Value
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source=MNT-MGR-2\SQLEX;Initial Catalog=MT;Integrated Security=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "UPDATE dbo.sparts SET [dbo.sparts.QtyonHand] = [dbo.sparts.QtyonHand] - zamnt WHERE [ComponentID] = zcomp"
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
Finally
con.Close()
gridRow.Cells(4).Value = "Yes"
End Try
I have tried it several different ways. It works just fine if I take out the zamnt and zcomp and put the actual number values that are in the variables. Please help I've been searching all day for a way to use the variables with this update query.
Thanks,
Stacy
You are probably looking for how to use parameters in ADO.NET. For your example, it can look like this:
cmd.Parameters.Add("#zamnt", zamnt);
cmd.Parameters.Add("#zcomp", zcomp);
Put these two lines anywhere before ExecuteNonQuery.
Because parameters need a # prefix, you would also need to change your query to say #zamnt instead of just zamnt, and same for zcomp:
cmd.CommandText = "UPDATE dbo.sparts SET [dbo.sparts.QtyonHand] = [dbo.sparts.QtyonHand] - #zamnt WHERE [ComponentID] = #zcomp"
In addition to using parameters, the "Using" statement closes the connection and disposes resources:
Dim zamnt As Integer = WopartsDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
Dim zcomp As Integer = gridRow.Cells(0).Value
Try
Using con As New SqlConnection("Data Source=MNT-MGR-2\SQLEX;Initial Catalog=MT;Integrated Security=True")
con.Open()
Using cmd As New SqlCommand
cmd.CommandText = "UPDATE dbo.sparts SET [dbo.sparts.QtyonHand] = [dbo.sparts.QtyonHand] - #zamnt WHERE [ComponentID] = #zcomp"
cmd.Parameters.AddWithValue("#zamt", zamnt)
cmd.Parameters.AddWithValue("#zcomp", zcomp)
cmd.ExecuteNonQuery()
End Using
End Using
Catch ex As Exception
MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
Finally
con.Close()
gridRow.Cells(4).Value = "Yes"
End Try
have u tried this?
Dim zamnt As Integer = WopartsDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
Dim zcomp As Integer = gridRow.Cells(0).Value
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source=MNT-MGR-2\SQLEX;Initial Catalog=MT;Integrated Security=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "UPDATE dbo.sparts SET [dbo.sparts.QtyonHand] = [dbo.sparts.QtyonHand] -" + zamnt + " WHERE [ComponentID] =" + zcomp
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
Finally
con.Close()
gridRow.Cells(4).Value = "Yes"
End Try