E
Earl
Been down in Tunica on vacation trying to win a poker tournament, but I'm
back to enjoy struggling with the datagrid data relation issue.
What I'm trying to do is relate the ZipCityState ID to the corresponding
name columns in the ZipCityState table. This works fine for displays
purposes, but I cannot do an Insert. Delete works as advertised.
With Insert, I get the error, "Cannot change ReadOnly property for the
Expression column".
In the code below, Form Load calls FillSuppliersGrid()
**********************************************
Private Sub FillSuppliersGrid()
DataGrid1.DataSource = Nothing
dsWinMatrix.Reset()
Dim strSQLServer As New SqlConnection(strConnString)
Dim cmdSuppliers As New SqlCommand("GetAllSuppliers", strSQLServer)
Dim cmdZipMatches As New SqlCommand("GetAllSupplierZips", strSQLServer)
cmdSuppliers.CommandType = CommandType.StoredProcedure
cmdZipMatches.CommandType = CommandType.StoredProcedure
cmdSuppliers.Connection = strSQLServer
cmdZipMatches.Connection = strSQLServer
strSQLServer.Open()
Dim da As New SqlDataAdapter(cmdSuppliers)
Dim daZip As New SqlDataAdapter(cmdZipMatches)
da.Fill(dsWinMatrix, "dtSuppliers")
daZip.Fill(dsWinMatrix, "dtSupplierZips")
dsWinMatrix.Tables("dtSuppliers").Columns("SupplierID").ColumnMapping =
MappingType.Hidden
dsWinMatrix.Tables("dtSuppliers").Columns("Tstamp").ColumnMapping =
MappingType.Hidden
dsWinMatrix.Tables("dtSuppliers").Columns("SupplierZipCityID").ColumnMapping
= MappingType.Hidden
dsWinMatrix.Tables("dtSupplierZips").Columns("ZipCityStateID").ColumnMapping
= MappingType.Hidden
Dim rel As DataRelation
rel = New DataRelation("CityStateZipSupplier", _
dsWinMatrix.Tables("dtSupplierZips").Columns("ZipCityStateID"), _
dsWinMatrix.Tables("dtSuppliers").Columns("SupplierZipCityID"), False)
dsWinMatrix.Relations.Add(rel)
dsWinMatrix.Tables("dtSuppliers").Columns.Add("City", GetType(String),
"Parent(CityStateZipSupplier).City")
dsWinMatrix.Tables("dtSuppliers").Columns.Add("State", GetType(String),
"Parent(CityStateZipSupplier).State")
dsWinMatrix.Tables("dtSuppliers").Columns.Add("Zip", GetType(String),
"Parent(CityStateZipSupplier).Zip")
DataGrid1.DataSource = dsWinMatrix.Tables("dtSuppliers")
cmSuppliers = CType(Me.BindingContext(dsWinMatrix.Tables("dtSuppliers")),
CurrencyManager)
dtSuppliers = dsWinMatrix.Tables("dtSuppliers")
AdjustTableStyle(dtSuppliers)
End Sub
Private Sub AddNewSupplier()
Dim strSQLServer As New SqlConnection(strConnString)
strSQLServer.Open()
dtSuppliers = dsWinMatrix.Tables("dtSuppliers")
dsWinMatrix.EnforceConstraints = False
Dim drNew As DataRow
Try
drNew = dtSuppliers.NewRow
If txtSupplier.Text <> "" Then
drNew("SupplierName") = txtSupplier.Text
Else
drNew("SupplierName") = DBNull.Value
End If
If txtAddress.Text <> "" Then
drNew("SupplierAddress") = txtAddress.Text
Else
drNew("SupplierAddress") = DBNull.Value
End If
If txtPhone.Text <> "" Then
drNew("SupplierPhone") = txtPhone.Text
Else
drNew("SupplierPhone") = DBNull.Value
End If
If txtTollFree.Text <> "" Then
drNew("SupplierPhone2") = txtTollFree.Text
Else
drNew("SupplierPhone2") = DBNull.Value
End If
If txtFax.Text <> "" Then
drNew("SupplierFax") = txtFax.Text
Else
drNew("SupplierFax") = DBNull.Value
End If
If txtRepresentative.Text <> "" Then
drNew("SupplierRep") = txtRepresentative.Text
Else
drNew("SupplierRep") = DBNull.Value
End If
If txtRepPhone.Text <> "" Then
drNew("SupplierRepPhone") = txtRepPhone.Text
Else
drNew("SupplierRepPhone") = DBNull.Value
End If
If txtURL.Text <> "" Then
drNew("SupplierWebsite") = txtURL.Text
Else
drNew("SupplierWebsite") = DBNull.Value
End If
If txtEmail.Text <> "" Then
drNew("SupplierEmail") = txtEmail.Text
Else
drNew("SupplierEmail") = DBNull.Value
End If
dtSuppliers.Rows.Add(drNew)
Dim da As New SqlDataAdapter
Dim dsChanges As DataSet
dsChanges = dsWinMatrix.GetChanges
da.InsertCommand = CreateSupplierInsert()
da.Update(dsChanges, "dtSuppliers")
dsWinMatrix.AcceptChanges()
Catch e_InsertException As System.Exception
MsgBox(e_InsertException.Message)
Throw e_InsertException
Finally
strSQLServer.Close()
End Try
End Sub
Private Function CreateSupplierInsert() As SqlCommand
Dim strSQLServer As New SqlConnection(strConnString)
Dim cmd As New SqlCommand("SupplierInsertCommand", strSQLServer)
cmd.CommandType = CommandType.StoredProcedure
Dim pc As SqlParameterCollection = cmd.Parameters
System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0,
Byte), "", System.Data.DataRowVersion.Current, Nothing))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierName",
System.Data.SqlDbType.VarChar, 50, "SupplierName"))
cmd.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@SupplierAddress",
System.Data.SqlDbType.VarChar, 50, "SupplierAddress"))
cmd.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@SupplierZipCityID",
System.Data.SqlDbType.Int, 4, "SupplierZipCityID"))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierPhone",
System.Data.SqlDbType.VarChar, 50, "SupplierPhone"))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierPhone2",
System.Data.SqlDbType.VarChar, 50, "SupplierPhone2"))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierFax",
System.Data.SqlDbType.VarChar, 50, "SupplierFax"))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierRep",
System.Data.SqlDbType.VarChar, 50, "SupplierRep"))
cmd.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@SupplierRepPhone",
System.Data.SqlDbType.VarChar, 50, "SupplierRepPhone"))
cmd.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@SupplierWebsite",
System.Data.SqlDbType.VarChar, 50, "SupplierWebsite"))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierEmail",
System.Data.SqlDbType.VarChar, 50, "SupplierEmail"))
Return cmd
End Function
back to enjoy struggling with the datagrid data relation issue.
What I'm trying to do is relate the ZipCityState ID to the corresponding
name columns in the ZipCityState table. This works fine for displays
purposes, but I cannot do an Insert. Delete works as advertised.
With Insert, I get the error, "Cannot change ReadOnly property for the
Expression column".
In the code below, Form Load calls FillSuppliersGrid()
**********************************************
Private Sub FillSuppliersGrid()
DataGrid1.DataSource = Nothing
dsWinMatrix.Reset()
Dim strSQLServer As New SqlConnection(strConnString)
Dim cmdSuppliers As New SqlCommand("GetAllSuppliers", strSQLServer)
Dim cmdZipMatches As New SqlCommand("GetAllSupplierZips", strSQLServer)
cmdSuppliers.CommandType = CommandType.StoredProcedure
cmdZipMatches.CommandType = CommandType.StoredProcedure
cmdSuppliers.Connection = strSQLServer
cmdZipMatches.Connection = strSQLServer
strSQLServer.Open()
Dim da As New SqlDataAdapter(cmdSuppliers)
Dim daZip As New SqlDataAdapter(cmdZipMatches)
da.Fill(dsWinMatrix, "dtSuppliers")
daZip.Fill(dsWinMatrix, "dtSupplierZips")
dsWinMatrix.Tables("dtSuppliers").Columns("SupplierID").ColumnMapping =
MappingType.Hidden
dsWinMatrix.Tables("dtSuppliers").Columns("Tstamp").ColumnMapping =
MappingType.Hidden
dsWinMatrix.Tables("dtSuppliers").Columns("SupplierZipCityID").ColumnMapping
= MappingType.Hidden
dsWinMatrix.Tables("dtSupplierZips").Columns("ZipCityStateID").ColumnMapping
= MappingType.Hidden
Dim rel As DataRelation
rel = New DataRelation("CityStateZipSupplier", _
dsWinMatrix.Tables("dtSupplierZips").Columns("ZipCityStateID"), _
dsWinMatrix.Tables("dtSuppliers").Columns("SupplierZipCityID"), False)
dsWinMatrix.Relations.Add(rel)
dsWinMatrix.Tables("dtSuppliers").Columns.Add("City", GetType(String),
"Parent(CityStateZipSupplier).City")
dsWinMatrix.Tables("dtSuppliers").Columns.Add("State", GetType(String),
"Parent(CityStateZipSupplier).State")
dsWinMatrix.Tables("dtSuppliers").Columns.Add("Zip", GetType(String),
"Parent(CityStateZipSupplier).Zip")
DataGrid1.DataSource = dsWinMatrix.Tables("dtSuppliers")
cmSuppliers = CType(Me.BindingContext(dsWinMatrix.Tables("dtSuppliers")),
CurrencyManager)
dtSuppliers = dsWinMatrix.Tables("dtSuppliers")
AdjustTableStyle(dtSuppliers)
End Sub
Private Sub AddNewSupplier()
Dim strSQLServer As New SqlConnection(strConnString)
strSQLServer.Open()
dtSuppliers = dsWinMatrix.Tables("dtSuppliers")
dsWinMatrix.EnforceConstraints = False
Dim drNew As DataRow
Try
drNew = dtSuppliers.NewRow
If txtSupplier.Text <> "" Then
drNew("SupplierName") = txtSupplier.Text
Else
drNew("SupplierName") = DBNull.Value
End If
If txtAddress.Text <> "" Then
drNew("SupplierAddress") = txtAddress.Text
Else
drNew("SupplierAddress") = DBNull.Value
End If
If txtPhone.Text <> "" Then
drNew("SupplierPhone") = txtPhone.Text
Else
drNew("SupplierPhone") = DBNull.Value
End If
If txtTollFree.Text <> "" Then
drNew("SupplierPhone2") = txtTollFree.Text
Else
drNew("SupplierPhone2") = DBNull.Value
End If
If txtFax.Text <> "" Then
drNew("SupplierFax") = txtFax.Text
Else
drNew("SupplierFax") = DBNull.Value
End If
If txtRepresentative.Text <> "" Then
drNew("SupplierRep") = txtRepresentative.Text
Else
drNew("SupplierRep") = DBNull.Value
End If
If txtRepPhone.Text <> "" Then
drNew("SupplierRepPhone") = txtRepPhone.Text
Else
drNew("SupplierRepPhone") = DBNull.Value
End If
If txtURL.Text <> "" Then
drNew("SupplierWebsite") = txtURL.Text
Else
drNew("SupplierWebsite") = DBNull.Value
End If
If txtEmail.Text <> "" Then
drNew("SupplierEmail") = txtEmail.Text
Else
drNew("SupplierEmail") = DBNull.Value
End If
dtSuppliers.Rows.Add(drNew)
Dim da As New SqlDataAdapter
Dim dsChanges As DataSet
dsChanges = dsWinMatrix.GetChanges
da.InsertCommand = CreateSupplierInsert()
da.Update(dsChanges, "dtSuppliers")
dsWinMatrix.AcceptChanges()
Catch e_InsertException As System.Exception
MsgBox(e_InsertException.Message)
Throw e_InsertException
Finally
strSQLServer.Close()
End Try
End Sub
Private Function CreateSupplierInsert() As SqlCommand
Dim strSQLServer As New SqlConnection(strConnString)
Dim cmd As New SqlCommand("SupplierInsertCommand", strSQLServer)
cmd.CommandType = CommandType.StoredProcedure
Dim pc As SqlParameterCollection = cmd.Parameters
System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0,
Byte), "", System.Data.DataRowVersion.Current, Nothing))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierName",
System.Data.SqlDbType.VarChar, 50, "SupplierName"))
cmd.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@SupplierAddress",
System.Data.SqlDbType.VarChar, 50, "SupplierAddress"))
cmd.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@SupplierZipCityID",
System.Data.SqlDbType.Int, 4, "SupplierZipCityID"))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierPhone",
System.Data.SqlDbType.VarChar, 50, "SupplierPhone"))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierPhone2",
System.Data.SqlDbType.VarChar, 50, "SupplierPhone2"))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierFax",
System.Data.SqlDbType.VarChar, 50, "SupplierFax"))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierRep",
System.Data.SqlDbType.VarChar, 50, "SupplierRep"))
cmd.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@SupplierRepPhone",
System.Data.SqlDbType.VarChar, 50, "SupplierRepPhone"))
cmd.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@SupplierWebsite",
System.Data.SqlDbType.VarChar, 50, "SupplierWebsite"))
cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SupplierEmail",
System.Data.SqlDbType.VarChar, 50, "SupplierEmail"))
Return cmd
End Function