I'm building a data entry windows form in VS.Net 2003 J# that requires
the data to be sent to a SQL Server table (the table's name is "Local")
when the form's "SAVE" button is clicked. I've built the form using
the windows form designer. There are six fields that will use SQL
statements to send data to the Local table, and none of the fields are
permitted to be null values. Two of the fields are comboboxes that are
populated through the use of their own respective datasets
(DisasterType and DwellingType); the datasets were built using the
forms designer OLEdbDataAdapter (Adapter3 belongs to DisasterType and
Adapter4 belongs to DwellingType). Three fields are
textboxes(FamilyName, WorkerName, and DamageDescGeneral) that require
the user to manually enter data. There is another combobox (Utilities)
where the user will select between "On" or "Off" choices. I'm trying
to point the Worker Name, FamilyName, Disaster Type, Dwelling Type,
Utilities, and General Description toward the Local table in the
database.
Since I built my DataAdapters using the Windows Forms wizard, the
adapter,command, and connection were all automatically built (see
attachment's Windows Forms Designer generated code). I then generated
datasets for DisasterType (dsDisaster1), DwellingType (dsDwelling1),
and Local (dsLocal1). The DisasterType and DwellingType comboboxes are
populated by data from other tables in the database, so I used their
respective tables as datasource/datamembers in the Properties window of
the form. I then used the Databindings property to bind those
comboboxes to their respective text fields in the Local dataset. Is
that the correct thing to do One of the things I'm looking for is the
correct procedure to pull data from a data table, populate a combobox,
and then use that data in a field in another table.
OleDbDataAdapter2 is the adapter for the Local dataset (dsLocal1). The
select, insert, update, and delete statements were all automatically
generated. And, I know there is a connection to the Local table
because one can see the datatypes to the table's corresponding fields
in the insert, update, and delete command methods (I've omitted these
parameters in the attachment to lessen the amount of code that one may
have to navigate through). In the oleDbInsertCommand2 method, I've
added the "NewID()" class to the Local_rowguid VALUE, so that a new
GUID would be created when a record was inserted into the table. I've
initialized the Local dataset (dsLocal1) in the cmdSave_Click event, so
that when the user clicks the Save button on the form, the data will be
sent from the dataset to the Local table. However, when I open the
Local table, there is no new data. The table is still empty.
I've included some of the source code from the DisasterType and
DwellingType comboxes that shows there DataSource/DataMembers, and
DataBindings. As well, I've included code from OleDataAdapter2 that
shows the SQL code that was generated, and the modification I made to
include "NewID()".
Thanks again for any assistance.
// DisasterType combobox
this.cboDisasterType.get_DataBindings().Add(new
System.Windows.Forms.Binding("Text", this.dsLocal1,
"Local.Disaster_ID"));
this.cboDisasterType.set_DataSource(this.dsDisasterType1);
this.cboDisasterType.set_DisplayMember("Disaster_Type.Disaster_ID");
//DisasterType dataset
this.dsDisasterType1.set_DataSetName("dsDisasterType");
this.dsDisasterType1.set_Locale(new
System.Globalization.CultureInfo("en-US"));
//DwellingType combobox
this.cboDwellingType.get_DataBindings().Add(new
System.Windows.Forms.Binding("Text", this.dsLocal1,
"Local.Dwelling_Type"));
this.cboDwellingType.set_DataSource(this.dsDwelling1);
this.cboDwellingType.set_DisplayMember("Dwelling.Dwelling_Type");
//Dwelling dataset
this.dsDwelling1.set_DataSetName("dsDwelling");
this.dsDwelling1.set_Locale(new
System.Globalization.CultureInfo("en-US"));
// oleDbConnection1
this.oleDbConnection1.set_ConnectionString("...table info...");
// oleDbDataAdapter2
this.oleDbDataAdapter2.set_DeleteCommand(this.oleDbDeleteCommand2);
this.oleDbDataAdapter2.set_InsertCommand(this.oleDbInsertCommand2);
this.oleDbDataAdapter2.set_SelectCommand(this.oleDbSelectCommand2);
this.oleDbDataAdapter2.set_UpdateCommand(this.oleDbUpdateCommand2);
// oleDbDeleteCommand2
this.oleDbDeleteCommand2.set_CommandText("DELETE FROM Local WHERE
(Local_rowguid = ) AND (ARC_Worker = ) AND (Description = ) AND
(Disaster_ID = ) AND (Dwelling_Type = ) AND (Family_Name = ) AND
(Utilities = )");
this.oleDbDeleteCommand2.set_Connection(this.oleDbConnection1);
// oleDbInsertCommand2
this.oleDbInsertCommand2.set_CommandText("INSERT INTO Local(ARC_Worker,
Description, Disaster_ID, Dwelling_Type, Family_Name, Utilities,
Local_rowguid) VALUES ( , , , , , , NewID()); SELECT ARC_Worker,
Description, Disaster_ID, Dwelling_Type, Family_Name, Utilities,
Local_rowguid FROM Local WHERE (Local_rowguid = NewID())");
this.oleDbInsertCommand2.set_Connection(this.oleDbConnection1);
// oleDbSelectCommand2
this.oleDbSelectCommand2.set_CommandText("SELECT ARC_Worker,
Description, Disaster_ID, Dwelling_Type, Family_Name, Utilities,
Local_rowguid FROM Local");
this.oleDbSelectCommand2.set_Connection(this.oleDbConnection1);
// oleDbUpdateCommand2
this.oleDbUpdateCommand2.set_CommandText("UPDATE Local SET ARC_Worker =
, Description = , Disaster_ID = , Dwelling_Type = , Family_Name =
, Utilities = , Local_rowguid = WHERE (Local_rowguid = )" +
" AND (ARC_Worker = ) AND (Description = ) AND (Disaster_ID = ) AND
(Dwelling_" +
"Type = ) AND (Family_Name = ) AND (Utilities = ); SELECT
ARC_Worker, Descript" +
"ion, Disaster_ID, Dwelling_Type, Family_Name, Utilities,
Local_rowguid FROM Local WHERE (Local_rowguid = )");
this.oleDbUpdateCommand2.set_Connection(this.oleDbConnection1);
private void frmLocal_Load (Object sender, System.EventArgs e)
{
//local dataset
dsLocal1.Clear();
oleDbDataAdapter2.Fill(dsLocal1);
oleDbConnection1.Close();
//populate disaster type combobox with data from Disaster_Type table
dsDisasterType1.Clear();
oleDbDataAdapter3.Fill(dsDisasterType1);
oleDbConnection1.Close();
//populate dwelling type combobox with data from Dwelling table
dsDwelling1.Clear();
oleDbDataAdapter4.Fill(dsDwelling1);
oleDbConnection1.Close();
}
private void cmdSave_Click (Object sender,
System.EventArgs e)
{
//fill dsLocal1 dataset
dsLocal1.Clear();
oleDbDataAdapter2.Fill(dsLocal1);
oleDbConnection1.Close();
//update Local table with data from dsLocal1 dataset
oleDbDataAdapter2.Update(dsLocal1);
oleDbConnection1.Close();
MessageBox.Show("Local Detailed Damage Assessment has been
updated.");
}
private void cmdCloseWindow_Click (Object sender, System.EventArgs e)
{
Close();
}

SQL novice: trouble connecting windows form with table
Pooja Jain