Inserting using FormView - c#

I am attempting to insert an items using a basic FormView control.
I am somewhat new to C# and its controls, so bear with me.
The FormView has standard entries like:
ItemName
ItemPrice
ItemSize
It also has controls that will be hidden from the user such as:
ItemDateCreated
ItemDatechanged
ItemChangedBy
These items I am attempting to modify their values before the Insert() takes place, so I have captured the event InsertButton_Click():
protected void InsertButton_Click(object sender, EventArgs e)
{
LinkButton btnInsert = (LinkButton)FormView1.FindControl("InsertButton");
TextBox txtDateAdded = (TextBox)FormView1.FindControl("ItemDateAddedTextBox");
TextBox txtDateChanged = (TextBox)FormView1.FindControl("ItemDateChangedTextBox");
TextBox txtChangedBy = (TextBox)FormView1.FindControl("ItemChangedByTextBox");
txtDateAdded.Text = DateTime.Now.ToString("MMMM dd, yyyy");
txtDateChanged.Text = DateTime.Now.ToString("MMMM dd, yyyy");
txtChangedBy.Text = HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToString();
tblItems.Insert();
}
It keeps telling me that the ItemName field is NULL, and throws an error, even though I can plainly see the value is being set in the textbox. Why is this value being thrown as NULL? Do I need to manually create the INSERT statement before I call the Insert()? How would I go about doing that?

Without seeing your .aspx code it's hard to tell for sure, but ItemName wasn't included in the code sample you gave. It sounds like your databinding syntax might not be correct, so the value in the textbox on your page isn't getting correctly mapped to the parameter in your Insert command.
Also, what you probably want to do instead of having hidden textbox controls is tie into the Inserting event on your datasource. This example assumes you're using a SqlDataSource control, so change according to your specific datasource:
private void On_Inserting(Object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters.Add(new SqlParameter("ItemDateAddedTextBox", DateTime.Now.ToString("MMMM dd, yyyy")));
...
}
You attach this handler in the definition of your datasource in your .aspx page. In the design view of your webpage, click on the datasource and in the properties window, click over to the Events tab. If you double-click the Inserting event, it should create the handler for you in your codebehind, and you just fill in your implementation.

If the field values are set from system then I would recommend doing that directly in Sql using getdate and not via asp.net code.
e.g.:
insert into mytable (...,..., ItemDateCreated) values (...,...,getdate())

Related

My method only works when called from one particular area of my application [duplicate]

I have tried to get an answer to this but so far no help has been able to do what I want it to.
I have this piece of code, which is meant to look at the selected row and output it's columns into the corresponding text boxes.
private void DataGridView01_SelectionChanged(object sender, EventArgs e)
{
if (DataGridView01.SelectedRows.Count > 0)
{
personIDTextBox.Text = DataGridView01.SelectedRows[0].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.SelectedRows[0].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.SelectedRows[0].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.SelectedRows[0].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.SelectedRows[0].Cells[6].Value.ToString();
}
}
When I launch the program, I get no errors but it doesn't output the data into the textbox. Anyone know what I am doing wrong?
HOOKING UP EVENTS:
It is the most basic thing you need to learn to code in VS. In short it means that the event name, here DataGridView01_SelectionChanged is connected to the event. To do so one can either use code or one inserts it into the correct slot of the events pane of the property tab. Select the DataGridView, open the events pane (the one with the flash) and locate the SelectionChanged event! Here insert the name of the event and you are done.
(I only have the German versions of VS installed..)
The result is reflected in the form_designer.cs file and it is the same thing (in reverse) as double clicking that spot and then filling in the generated code stub..
Controls have many events; one is the default event and this can be generated by double clicking the control itself in the designer. But eventually you will need all 3 ways to generate and hook up the events, (as well as sometimes removing them.)
I use a slightly different approach when trying to get data from a datagridview.
Try doing personIDTextBox.Text = DataGridView01.SelectedCells[0].Value.ToString();
but instead of the event being on selection change, switch to CellClick and change the property of the the datagridview row selection property to full row select. after that you can change the SelectedCell[0] number to match whichever cell you want
If you want to display the datagridview selected rows into corresponding textboxes, fine the below steps ,
Step 1:
1. Change the DataGridView Selection mode to FullRowSelect in Datagridview property.
2. Create the cell click event in Data grid view using property.
enter image description here
3. Write the below code and test it, It may helpful
private void DataGridView01_CellClick(object sender,DataGridViewCellEventArgs e)
{
if (DataGridView01.Rows.Count > -1)
{
PersonIdTextBox.Text=DataGridView01.Rows[e.RowIndex].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.Rows[e.RowIndex].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.Rows[e.RowIndex].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[6].Value.ToString();
}
}

Copy DataGridView values to TextBox

I have tried to get an answer to this but so far no help has been able to do what I want it to.
I have this piece of code, which is meant to look at the selected row and output it's columns into the corresponding text boxes.
private void DataGridView01_SelectionChanged(object sender, EventArgs e)
{
if (DataGridView01.SelectedRows.Count > 0)
{
personIDTextBox.Text = DataGridView01.SelectedRows[0].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.SelectedRows[0].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.SelectedRows[0].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.SelectedRows[0].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.SelectedRows[0].Cells[6].Value.ToString();
}
}
When I launch the program, I get no errors but it doesn't output the data into the textbox. Anyone know what I am doing wrong?
HOOKING UP EVENTS:
It is the most basic thing you need to learn to code in VS. In short it means that the event name, here DataGridView01_SelectionChanged is connected to the event. To do so one can either use code or one inserts it into the correct slot of the events pane of the property tab. Select the DataGridView, open the events pane (the one with the flash) and locate the SelectionChanged event! Here insert the name of the event and you are done.
(I only have the German versions of VS installed..)
The result is reflected in the form_designer.cs file and it is the same thing (in reverse) as double clicking that spot and then filling in the generated code stub..
Controls have many events; one is the default event and this can be generated by double clicking the control itself in the designer. But eventually you will need all 3 ways to generate and hook up the events, (as well as sometimes removing them.)
I use a slightly different approach when trying to get data from a datagridview.
Try doing personIDTextBox.Text = DataGridView01.SelectedCells[0].Value.ToString();
but instead of the event being on selection change, switch to CellClick and change the property of the the datagridview row selection property to full row select. after that you can change the SelectedCell[0] number to match whichever cell you want
If you want to display the datagridview selected rows into corresponding textboxes, fine the below steps ,
Step 1:
1. Change the DataGridView Selection mode to FullRowSelect in Datagridview property.
2. Create the cell click event in Data grid view using property.
enter image description here
3. Write the below code and test it, It may helpful
private void DataGridView01_CellClick(object sender,DataGridViewCellEventArgs e)
{
if (DataGridView01.Rows.Count > -1)
{
PersonIdTextBox.Text=DataGridView01.Rows[e.RowIndex].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.Rows[e.RowIndex].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.Rows[e.RowIndex].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[6].Value.ToString();
}
}

FieldInfo.CaptionStyle in custom form control not working

I have created a custom form control for use in my Kentico bizform using asp.net and I want to change the field caption style of another field in the form depending upon the value in my custom form control field. So, this is what I have done:
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if(this.Value == "1")
{
FormEngineUserControl formItem = (FormEngineUserControl)this.Form.FieldControls["Other"];
formItem.FieldInfo.CaptionStyle = "font-weight:bold";
}
}
However, the field caption in the form doesn't seem to get bolded. I tried testing if the event even fires and it does. Infact, if I try something like formItem.Text = "Something" then the texbox gets filled with "Something". While debugging I also noticed that the field caption style does get changed to "font-weight: bold" but that doesn't show on the form. So, there is something wrong with the captionstyle property or the way I am using it. How do I get it to work?
(Please note that the field control "Other" is a text box input)
It's probably too late in Page's lifecycle and the control has already been rendered. Try to set the CaptionStyle earlier (e.g. in control's OnLoad or OnInit) then you'll know with certainty.

Accessing ItemTemplate from codebehind

I have a GView and I am populating rows from code behind. However, in the RowUpdating event I want to access the changes done by the user and store that change into a string. Here is my code:
protected void gvShowComm_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
System.Web.UI.WebControls.TextBox myBox = gvShowComm.Rows[e.RowIndex].FindControl("PlanName") as System.Web.UI.WebControls.TextBox;
string s = myBox.Text;
gvShowComm.DataBind();
}
string s is still showing me the OLD text which is populated before. I want to store new string which user entered. How do I do that?
make sure you repopulate rows and call DataBind at least on PageLoad, preferably on OnIniti without doing that, you always get values from viewstate (if you didnt disable it). at a last resort, you can investigate Request.Form and find if you recieved new value properly
Can't you use e.NewValues[] . The result that you are getting is probably e.OldValues[]

How to change the Combobox.Text property when Combobox is bound to data?

My scenerio is like this:
At runtime, I bind ToolStripComboBox to array of struct:
cbxTimes.ComboBox.DataSource = PlayTimeLengths;
cbxTimes.ComboBox.DisplayMember = "Description";
cbxTimes.ComboBox.ValueMember = "Minutes";
The DropDownStyle of ToolStripCombobox is set to DropDown.
Everything is working fine, I can select values from the dropdown list and I can write text in the control.
However I wanted to prevent user from pressing some controls and alternate the Text property when some other controls are pressed.
I am trying to accomplish this in KeyPress event:
private void cbxTimes_KeyPress(object sender, KeyPressEventArgs e)
{
var cbxSender = ((ToolStripComboBox)sender).ComboBox;
string S = cbxSender.Text;
//some operations on the S variable
cbxSender.Text = S;
e.Handled = true;
} // breakpoint here shows that cbxSender.Text is not changed to S!
So the Text property has not been changed but I didn't get any exception.
However, if I run the program further (I quit from the debugging) I see that the Text property is changed - to be more specific. I see the text from S inside the control.
Now, imagine that I press any key for the second time, and again I am in the debugger in the same event:
private void cbxTimes_KeyPress(object sender, KeyPressEventArgs e)
{
var cbxSender = ((ToolStripComboBox)sender).ComboBox;
string S = cbxSender.Text; // this time breakpoint is here
//some operations on the S variable
cbxSender.Text = S;
e.Handled = true;
} // breakpoint here shows that cbxSender.Text is not changed to S!
But this time I put breakpoint on the second line and after examining the Text property I see that it still has not changed. Despite the fact that I've altered it on the first time when the event was fired up and the altereted text is visible in the control. But under debugger I see different value, I see value that has been set up at the begining. Value which belongs to the array of structs.
SO what can I do to overcome this problem?
Honestly this is one of the things I hate about Windows Forms databinding. In WPF you would not bind to the objects directly, you'd bind to a "ViewModel" object which encapsulated this view logic you have and bind to it instead.
My workaround to all of this would be to just not use databinding for this case at all and manually populate the items as needed. I can understand why you might be having this problem. If you had updated your underlying bound object's .Text (or whatever causes ToString() to display the value), you would probably see the new value you'd set, but that upsets the semantics of your underlying objects, which is Not A Good Thing.

Categories

Resources