[RESOLVED] Stored Procedure for DataGridView
I got an SQL SERVER database and a VB.NET form containing couple of datagridviews and some comboboxes
I linked the grids and boxes with some views and fields from database and they work just fine when form loads
My problem is when I select a value from a combo I want to execute a stored procedure to display certain records in the grids
So the steps I did for that are
in dataset's design view - the tableadapter of a certain table ... Add Querry ... Use existing stored procedure (I made a stored procedure in SQL SERVER)
I selected the stored procedure and selected Tabular Data because I want more than 1 value returned ... I named the methods MyFill and MyGetDataBy and all seem perfect
The problem is in the SelectedIndexChanged method where I want to refresh the datagrids with new values
Me.dbGrid1.DataSource = Me.ComenziTableAdapter.MyGetDataBy("Acr") - Acr is a stored procedure parameter
the error message is :
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
thank you
Re: Stored Procedure for DataGridView
I replaced the Me.dbGrid1.DataSource = Me.ComenziTableAdapter.MyGetDataBy("Acr") code with
Dim xDataSet As DataSet = New DataSet
Dim sCconnect = "Data Source=sqlexpress;Initial Catalog=mydatabase;Integrated Security=True"
Dim xSqlConnection As SqlConnection = New SqlConnection(sCconnect)
Dim xSqlCommand As SqlCommand = New SqlCommand("SP_Principal", xSqlConnection)
xSqlCommand.CommandType = CommandType.StoredProcedure
xSqlCommand.Parameters.Add("@criteriu", SqlDbType.VarChar, 100)
xSqlCommand.Parameters("@criteriu").Value = "Acr"
xSqlCommand.Connection.Open()
Dim xSqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(xSqlCommand)
xSqlDataAdapter.Fill(xDataSet)
xSqlDataAdapter.Dispose()
dbGrid1.DataSource = xDataSet
dbGrid1.DataMember = xDataSet.DataSetName
Dim a As String
a = Trim(xDataSet.Tables(0).Rows(0).Item(0).ToString())
dbGrid1.Refresh()
and guess what ... a variable has the right value from database and the grid doesnt raise any errors but display 1 empty row
Re: Stored Procedure for DataGridView
I created an TableAdapter for the stored procedure and used this single line of code
dbGrid1.DataSource = Me.SP_Principal1TableAdapter.GetData("Acr")
it works just fine