Overload resolution failed because no accessible 'Item' accepts this number of arguments - sql

Making a small page that calls a SQL stored Proc with only one parameter, which the user would enter in a text box. In declaring that parameter, it is giving me the error above. Can anyone assist? The error is on the CMD.Parameters line..
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim CMD As New SqlCommand("daily_revenue_rerun")
CMD.Parameters("#date", SqlDbType.DateTime).value = TextBox1.Text
Dim connection As SqlConnection = New SqlConnection
connection.ConnectionString = "Data Source=azda-sql0;Persist Security Info=True;User ID=sa;Password=Sql#dm!n;Initial Catalog=RevenueTrakSQL"
CMD.Connection = connection
CMD.CommandType = CommandType.StoredProcedure
Dim Adapter As New SqlDataAdapter(CMD)
Dim DS As DataSet
Adapter.Fill(DS)
connection.Close()
End Sub
Thanks for any assistance..

Look at your parameter binding. What I can see is that your stored proc daily_revenue_rerun expects a DATETIME datatype variable whereas you are passing a string
CMD.Parameters("#date", SqlDbType.DateTime).value = TextBox1.Text

Related

VB.Net Insert Into SQL Database

I was hoping someone could explain this a bit better for me.
I have a visual studio project and created the database in the project: Project >> Add Item >> Service Database. I have a form with a textbox that I am trying to insert data into and I have looked up how to do this and there are things like SQLCommand or ExecuteNonQuery are not an option I have.
Since the database is associated with the project I don't know if I even need to do that part but I haven't seen anything to the contrary. I don't want to hard code in a server connection if I can avoid it because I am hoping this will become an application.
This is my code so far
Private Sub btnAddNewSpellSchool_Click(sender As Object, e As EventArgs) Handles btnAddNewSpellSchool.Click
Dim sqlCMD As String
Dim text As String
text = Me.txtAddSpellSchool.Text
sqlCMD = "INSERT INTO tblList_Spell_Config_SpellSchool (spellSchool) VALUES('" & text & "')"
End Sub
This is what I have been seeing
Dim DA As SqlDataAdapter = New SqlDataAdapter
Dim Parm As New SqlParameter
DA.InsertCommand = New SqlCommand("Insert Into tbl1(fld0, fld1, fld2) Values(#fld0, #fld1, #fld2)", conn)
Parm = DA.InsertCommand.Parameters.Add(New SqlParameter ("#fld0", NVarChar, 50, "fld0"))
Parm = sqlDA.InsertCommand.Parameters.Add(New SqlParameter ("#fld1", SqlDbType.NVarChar, 50, "fld1"))
Parm = sqlDA.InsertCommand.Parameters.Add(New SqlParameter ("#fld2", SqlDbType.NVarChar, 50, "fld2"))
DA.Update(dataset1, "tbl1")
Imports System.Data
Imports System.Data.SqlClient
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myconn As SqlConnection
Dim mycmd As SqlCommand
Dim qry As String
qry = "Insert Into tblList_Spell_Config_SpellSchool (spellSchool) Values('Air')"
myconn = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\jpac0\Dropbox\Dungeon and Dragons\DND_Application\DND_ExperienceBuilder\DND_ExperienceBuilder\DND_ExperienceBuilderDB.mdf;Integrated Security=True;Connect Timeout=30")
myconn.Open()
mycmd = New SqlCommand(qry, myconn)
mycmd.ExecuteNonQuery()
myconn.Close()
End Sub

Fatal error encountered during command execution.'

I want to save the rows of datagridview into the database. I am using editbtn click button, when I press the button it gives me the following error: "Fatal error encountered during command execution." Here is the code which I am using
Private Sub Supplier_Load(sender As Object, e As EventArgs) Handles MyBase.Load
conn.ConnectionString = "Server=127.0.0.1;Database=pembelian;Uid=root;Pwd=;"
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
disp_data()
End Sub
Public Sub disp_data()
cmd = conn.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from supplier"
cmd.ExecuteNonQuery()
Dim dt As New DataTable()
Dim da As New MySqlDataAdapter(cmd)
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Sub edit_btn_Click(sender As Object, e As EventArgs) Handles edit_btn.Click
Dim query As String = "updates supplier set nama=#nama, alamat=#alamat where npwp=#npwp"
cmd = New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("#nama", nama.Text)
cmd.Parameters.AddWithValue("#alamat", alamat.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("Data berhasil di update")
disp_data()
End Sub
First of all, it's not good to re-use the same connection object throughout your application. There is a feature in ADO.Net called connection pooling, where the MySqlConnection object you use in your code is actually a simple wrapper for the real underlying connection. These real connections are much heavier and more expensive to manage. They handle the real work of authentication, getting network socket resources, negotiating with the server, etc.
When you try to re-use the same connection object, you are optimizing the small thing (MySqlConnection) at the expense of the big thing (the real underlying connections). Don't do that.
Instead, you really are much better off creating a new connection for most queries, and then returning it to the pool as quickly as possible. This is normally handled with a Using block.
That out of the way I can look at the actual question. I noticed the #npwp parameter is not defined in the last method. Guessing at the name of the appropriate field, you want something more like this:
Private Sub edit_btn_Click(sender As Object, e As EventArgs) Handles edit_btn.Click
Dim query As String = "updates supplier set nama=#nama, alamat=#alamat where npwp=#npwp"
Using conn As New MySqlConnection("Server=127.0.0.1;Database=pembelian;Uid=root;Pwd=;")
Using cmd As New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("#nama", nama.Text)
cmd.Parameters.AddWithValue("#alamat", alamat.Text)
cmd.Parameters.AddWithValue("#npwp", npwp.Text)
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
MessageBox.Show("Data berhasil di update")
disp_data()
End Sub
Public Sub disp_data()
Dim dt As New DataTable()
Dim query As String = "select * from supplier"
Using conn As New MySqlConnection("Server=127.0.0.1;Database=pembelian;Uid=root;Pwd=;")
Using cmd As New MySqlCommand(query, conn)
Using da As New MySqlDataAdapater(cmd)
da.Fill(dt)
End Using
End Using
End Using
DataGridView1.DataSource = dt
End Sub

The SelectCommand property has not been initialized before calling 'Fill' problem

The SelectCommand property has not been initialized before calling 'Fill' problem:
Private Sub DeleteButton_Click(sender As Object, e As EventArgs) Handles DeleteButton.Click
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Dim row As New Integer
Try
ds = New DataSet
tables = (ds.Tables)
da = New OleDbDataAdapter
da.Fill(ds, "Booking")
Dim cmdstr As String = "delete * from [Booking] where ID = " & DataGridView1.SelectedRows(0).Cells(0).Value.ToString()
Dim cmd As New OleDbCommand(cmdstr, objCon)
da.SelectCommand = cmd
objCon.Open()
cmd.ExecuteNonQuery()
objCon.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Change
da.SelectCommand = cmd
With
da.DeleteCommand = cmd
Reguarding the Dim statements at the beginning of your code; you never use tables, source1 or row in your code so, there is no point in declaring them.
On to the Try You assign a New DataSet to ds. This is empty so assigned the Tables collection to tables makes no sense. This will be empty also.
Next you create a New DataAdapter. A new DataAdapter has no select command so it will be unable to .Fill anything.
Then you assign a Delete command to the SelectCommand property of a DataAdapter. This makes no sense.
Finally you execute you command. This should work if wasn't for the other code.
Keep you Database objects local so you can control their closing and disposing. Using...End Using takes care of this for you. The Using also acts as a Dim statement so it also declares you variables. You can include more than one variable with a single Using by separating them by a comma.
I guessed at the datatype of you ID field. Check your database.
Private Sub DeleteButton_Click(sender As Object, e As EventArgs) Handles DeleteButton.Click
Try
Using cn As New OleDbConnection("Your connection string"),
cmd As New OleDbCommand("delete * from [Booking] where ID = #ID", cn)
cmd.Parameters.Add("#ID", OleDbType.Integer).Value = DataGridView1.SelectedRows(0).Cells(0).Value
cn.Open()
cmd.ExecuteNonQuery()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

How To Populate a DropDown List From a DataSet?

I am trying to populate an ASP dropdown list in vb.net with the results from a stored procedure returned in a data set. I was wondering if anyone knew the vb.net code to populate the dropdown list?
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim connString As String = "Server=MYCOMPUTER\SQLEXPRESS;Database=scales;Trusted_Connection=True"
Dim myConn As New SqlConnection(connString)
myConn.Open()
Dim da As New SqlDataAdapter("select scaleName from scales", myConn)
Dim dt As New DataTable
da.Fill(dt)
ComboBox1.DisplayMember = "scaleName"
ComboBox1.DataSource = dt
myConn.Close()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
IF Not IsPostback then
PopulateDropdown()
End IF
End Sub
Private Sub PopulateDropDown()
Dim connString As String = "Server=MYCOMPUTER\SQLEXPRESS;Database=scales;Trusted_Connection=True"
Dim myConn As New SqlConnection(connString)
myConn.Open()
Dim da As New SqlDataAdapter("select ScaleId, scaleName from scales", myConn)
Dim dt As New DataTable
da.Fill(dt)
Me.ComboBox1.DataTextField = "scaleName "
Me.ComboBox1.DataValueField = "ScaleId"
Me.ComboBox1.DataSource = dt
Me.ComboBox1.DataSourceID = String.Empty
Me.ComboBox1.DataBind()
myConn.Close()
End Sub
In order to show the data in the DropDownList control, you can use the following Code. To use the results of a Stored Procedure, you need to create the SELECT command:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim dt As New DataTable
Dim connString As String = "Server=MYCOMPUTER\SQLEXPRESS;Database=scales;Trusted_Connection=True"
Using myConn As New SqlConnection(connString)
myConn.Open()
Using cmd = myConn.CreateCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "dbo.uspMyStoredProc"
cmd.Parameters.AddWithValue("#MyInputParam", 123)
Using da As New SqlDataAdapter(cmd)
da.Fill(dt)
End Using
End Using
End Using
ComboBox1.DisplayMember = "scaleName"
ComboBox1.DataSource = dt
ComboBox1.DataBind()
End If
' ...
End Sub
I've adjusted the following things:
Usually you only need to bind the data on the initial request. Therefore, the if statement at the beginning checks the IsPostBack property.
In order to close and dispose the connection and the data datapter reliably, I've added some using statements.
In order to access the stored procedure, I've created a SqlCommand and set the CommandType to StoredProcedure. The CommandText is set to the name of the Stored Procedure. In the sample, I've also added a parameter named MyInputParam that is sent to the Stored Procedure.

“The ConnectionString property has not been initialized” error in VB.NET

Every time I tried to connect to the database it give me this error "The ConnectionString property has not been initialized"
How can i avoid this error and make it work?
Here are my codes:
Imports System.Data.OleDb
Public Class frmLoginPage
Dim con As New OleDbConnection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("Select*from tblPassword", con)
da.Fill(dt)
Dim newRow As DataRow = dt.NewRow
With newRow
If .Item("Username") = txtUsername.Text And .Item("Password") = txtPassword.Text Then frmMainMenu.ShowDialog() Else MsgBox("The username or password you entered was incorrect, please try again!", MsgBoxStyle.Critical, "Information")
End With
End Sub
You have instantiated an OleDbConnection object, but you haven't set the connectionstring property so it doesn't know where it should connect to. You also haven't opened it. Your code should look something like:
Dim myConnection As OleDbConnection = New OleDbConnection()
myConnection.ConnectionString = myConnectionString
myConnection.Open()
' execute queries, etc
myConnection.Close()
This Problem Occurs when you donot consider making a new connection
The solution is very simple, all you have to do is to make
Dim conString as string = "Your connection string here"
Dim con As new OleDbConnection
con.connectionSting= ConSting
con.open()
'now simply write the query you want to be executed
'At the end write
con.close()
this will solve your problem.
if it doesn't solve your problem post reply I will try to solve it.
You never assigned your connection string to the connection object, just like the error is saying.
Insert a line setting the connection string before con.open.
Con.connectionstring = connection
Con.Open()
Or better yet, change your using statement as follows
Dim Connection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=G:\VB Project\Library Catalog System\Library Catalog System\library.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True"
Using Con As New SqlConnection(connection)