Update table using bound text box

  • Thread starter Thread starter gj
  • Start date Start date
G

gj

Hi,

I'm trying to update a sql database from a web form using text boxes.
I'm trying to learn C# on my own so I am at a complete loss. I created
my sql connection, data adapter, dataset and data view in the visual
studio designer. I'm trying to keep a history of the record so instead
of editing the record I insert a new record with my changes. Instead
of the changes, it inserts the orginal record. Below is the part of
the code. Any help would be appreiated.

else if (txtTask.Text.Trim() == "1")
{
//assign values to variables
valName = txtName.Text.ToString();
valStatus = txtStatus.Text.ToString();
valQuestionsPerPage = short.Parse(txtQuestionsPerPage.Text);
valRetakeOption = int.Parse(drpRetakeOption.SelectedValue);
valPassGrade = short.Parse(txtPassGrade.Text);
valAuthor = txtAuthor.Text.ToString();
valUserID = "jongay";
valSortType = int.Parse(drpSortType.SelectedValue);
valNotifyOptions = int.Parse(drpNotifyOptions.SelectedValue);
valGradeScale = int.Parse(drpGradeScale.SelectedValue);
valNumber = int.Parse(txtAQQuizID.Text);
valEffectiveDate = DateTime.Parse(txtEffectiveDate.Text);
valActivityDate = DateTime.Now;

dsMain.QuizRow drv = dsMain1.Quiz.NewQuizRow();

drv.Quiz_Name = valName;
drv.Quiz_Status = valStatus;
drv.Quiz_Questions_PerPage = valQuestionsPerPage;
drv.Quiz_PassGrade = valPassGrade;
drv.Quiz_User_ID = valUserID;
drv.Quiz_Author = valAuthor;
drv.Quiz_Activity_Date = valActivityDate;
drv.SortType_ID = valSortType;
drv.NotifyOptions_ID = valNotifyOptions;
drv.GradeScale_ID = valGradeScale;
drv.RetakeOption_ID = valRetakeOption;
drv.Quiz_Effective_Date = valEffectiveDate;
drv.Quiz_Number = valNumber;

dsMain1.Quiz.AddQuizRow(drv);

daQuiz.Update(dsMain1, "Quiz");

} //end if
 
I think you do a DataBind() of the Controls with the DB before updating it

Let me guess:

In the OnLoad method you fill the DataSet and bind the Controls with it
In the OnClick event you try to update the DB

I'm right?
 
You are right. I do fill the DataSet in the Page_Load method. Then I
do this.Databind();. When I fill out the web form and click Submit,
the value in the text box changes back to the orginal value before it
accesses my code in the OnClick event. I bound the textbox to the
DataView in Visual Studio designer using the textbox properties. Did I
do this wrong? Do I need to redo the DataBind() in the OnClick event?

Thanks
 
This is my way:
In the onload method:
-fill the dataset
-if page is postback get the value from the controls and add/update them in
the dataset
-if page is not postback bind controls with the dataset

In the onclick method (for example 'save')
-Use dataadapter to update DB
-Bind controls with the dataset

On_Load()
{
...
FillDataSetWithDataAdapter();
if (!Page.IsPostback) BindControls();
else GetValueFromControlsAndUpdateDataSet();
...
}

OnClick_Save()
{
DataAdapterUpdateDataSet();
BindControls();
}

OnClick_New()
{
AddANewRowToTheDataSet();
BindControls();
}

BindControls()
{
... //here bind the controls with the dataset
}
 
Thanks, I really appreciate your help.

Gayle

Zürcher See said:
This is my way:
In the onload method:
-fill the dataset
-if page is postback get the value from the controls and add/update them in
the dataset
-if page is not postback bind controls with the dataset

In the onclick method (for example 'save')
-Use dataadapter to update DB
-Bind controls with the dataset

On_Load()
{
...
FillDataSetWithDataAdapter();
if (!Page.IsPostback) BindControls();
else GetValueFromControlsAndUpdateDataSet();
...
}

OnClick_Save()
{
DataAdapterUpdateDataSet();
BindControls();
}

OnClick_New()
{
AddANewRowToTheDataSet();
BindControls();
}

BindControls()
{
... //here bind the controls with the dataset
}
 
Back
Top