Category Archives: ListView

[RESOLVED] is it possible to edit, update and delete in a textbox in asp.net

My question is

is it possible to edit, update and delete data in a textbox in asp.net using c# language. The data is displayed in textbox from ms access database. Am not using gridview to display the data. I am displaying the data in corresponding textbox. I want to perform edit, update and delete operations in the textbox itself in asp.net using c# language. If so please help me with the code.


Thanks in advance,


Avinash


Sure it's possible. 

string textTobeUpdated = TextBox1.Text;


using (var conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..."))
using (var cmd = conn.CreateCommand())
{
	cmd.CommandText = "UPDATE table SET content=@content where id=1";
	cmd.Parameters.AddWithValue("@content",content);
	conn.Open();
	cmd.ExecuteNonQuery();
}

Hi,

Could you please guide me how to send go about it through code.


Thanks in advance,


Avinash


Please reply. I need it urgently. Please help me with the code to edit, update and delete data in a tetxbox. Am using ms access database & the data are being displayed in a textbox in asp.net using c# language. Am not using gridview. Is it possible to edit, update and delete data in a textbox. If so please provide me the code to do so.

 

Thanks in advance,

 

Avinash

I've sent you an example above. Can't you use it, or what is the problem?

May I know, what's the reason to NOT use GridView?

The databound controls like GridView, ListView, Repeater etc are all designed to do data manipulation

among a large number of records.

textbox helps in more data manipulation and control. am just learing bout asp.net grid view helps a lot but we just provide binding functionality and the rest is taken care of. so i want more data control. so i wanted the code to edit, update and delete data from textbox in asp.net. First of all is it possible to do it.

 

Thanks in advance,

 

Avinash

Hi avinash.iyengar,

Here is the code how to display the data into the texbox.

SqlConnection cn ;

SqlCommand cmd ;

SqlDataReader rdr ;

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

try

{

cn = new SqlConnection("server=localhost;uid=sa;pwd=;database=northwind");

cmd = new SqlCommand( "select * from Products where productid=1 ", cn);

cn.Open();

rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection );

rdr.Read ();

TextBox1.Text =rdr["ProductId"].ToString ();

TextBox2.Text =rdr["ProductName"].ToString ();

}

catch (Exception ex)

{

Response.Write (ex.Message.ToString ());

}

finally

{

rdr.Close();

cn.Close();

}

}


Here is the code how to update the textbox value into database.

  protected void Update_Click(object sender, EventArgs e)
      {
        con.Open();
        string insert = "INSERT into Sales.SalesOrderHeader ( OrderDate, DueDate, ShipDate, SalesOrderNumber, TotalDue)" + "values(@OrderDate, @DueDate, @ShipDate, @SalesOrderNumber, @TotalDue)";
        SqlCommand cmd = new SqlCommand(insert, con);
       

        //bind the parameters
       cmd.Parameters.AddWithValue("@OrderDate",TextBox2);
       cmd.Parameters.AddWithValue("@DueDate", TextBox3);
       cmd.Parameters.AddWithValue("@ShipDate", TextBox4);
       cmd.Parameters.AddWithValue("@SalesOrderNumber", TextBox5);
       cmd.Parameters.AddWithValue("@TotalDue", TextBox6);
        cmd.ExecuteNonQuery();
        con.Close();
       
      }

 

I think you better look at some of the tutorials in here...

http://www.asp.net/web-forms/data

especially this....

http://www.asp.net/data-access/tutorials/an-overview-of-inserting-updating-and-deleting-data-cs

Thanks.

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 91: {
Line 92:
Line 93: if (i < ds.Tables[0].Rows.Count - 1)
Line 94: {
Line 95: i++;

this is error i get i press next button in asp.net web page.

My next button code is here:

protected void btnNext_Click(object sender, EventArgs e)
{

if (i < ds.Tables[0].Rows.Count - 1)
{
i++;

txtCustomerID.Text = ds.Tables[0].Rows[i]["CustomerID"].ToString();
txtCompanyName.Text = ds.Tables[0].Rows[i]["CompanyName"].ToString();
txtContactName.Text = ds.Tables[0].Rows[i]["ContactName"].ToString();
txtAddress.Text = ds.Tables[0].Rows[i]["Address"].ToString();
txtCity.Text = ds.Tables[0].Rows[i]["City"].ToString();
txtRegion.Text = ds.Tables[0].Rows[i]["Region"].ToString();
txtCountry.Text = ds.Tables[0].Rows[i]["Country"].ToString();
txtPhone.Text = ds.Tables[0].Rows[i]["Phone"].ToString();

}
else
{
Response.Write("There are no records further!! You have reached the end of records in this category");
}

}

The same error occurs for first, previous and last button press event also.

their codes are as follows:

protected void btnFirst_Click(object sender, EventArgs e)
{
if (ds.Tables[0].Rows.Count > 0)
{
i = 0;
txtCustomerID.Text = ds.Tables[0].Rows[i]["CustomerID"].ToString();
txtCompanyName.Text = ds.Tables[0].Rows[i]["CompanyName"].ToString();
txtContactName.Text = ds.Tables[0].Rows[i]["ContactName"].ToString();
txtAddress.Text = ds.Tables[0].Rows[i]["Address"].ToString();
txtCity.Text = ds.Tables[0].Rows[i]["City"].ToString();
txtRegion.Text = ds.Tables[0].Rows[i]["Region"].ToString();
txtCountry.Text = ds.Tables[0].Rows[i]["Country"].ToString();
txtPhone.Text = ds.Tables[0].Rows[i]["Phone"].ToString();
}

else
{
Response.Write("You have reached the First record in this category");
}
}

protected void btnPrevious_Click(object sender, EventArgs e)
{
if (i == ds.Tables[0].Rows.Count - 1 || i != 0)
{
i--;

txtCustomerID.Text = ds.Tables[0].Rows[i]["CustomerID"].ToString();
txtCompanyName.Text = ds.Tables[0].Rows[i]["CompanyName"].ToString();
txtContactName.Text = ds.Tables[0].Rows[i]["ContactName"].ToString();
txtAddress.Text = ds.Tables[0].Rows[i]["Address"].ToString();
txtCity.Text = ds.Tables[0].Rows[i]["City"].ToString();
txtRegion.Text = ds.Tables[0].Rows[i]["Region"].ToString();
txtCountry.Text = ds.Tables[0].Rows[i]["Country"].ToString();
txtPhone.Text = ds.Tables[0].Rows[i]["Phone"].ToString();
}
else
{
Response.Write("You have reached the begining of the records in the category!!");
}
}

protected void btnLast_Click(object sender, EventArgs e)
{
i = ds.Tables[0].Rows.Count - 1;

txtCustomerID.Text = ds.Tables[0].Rows[i]["CustomerID"].ToString();
txtCompanyName.Text = ds.Tables[0].Rows[i]["CompanyName"].ToString();
txtContactName.Text = ds.Tables[0].Rows[i]["ContactName"].ToString();
txtAddress.Text = ds.Tables[0].Rows[i]["Address"].ToString();
txtCity.Text = ds.Tables[0].Rows[i]["City"].ToString();
txtRegion.Text = ds.Tables[0].Rows[i]["Region"].ToString();
txtCountry.Text = ds.Tables[0].Rows[i]["Country"].ToString();
txtPhone.Text = ds.Tables[0].Rows[i]["Phone"].ToString();

Response.Write("You have reached the end of records in this category!! There are no records further!!");
}
Please help me in this regard. I really need help in this.

Thanks in advance,

Avinash

I would still say -- give GridView a try.

These databound controls are meant to take care of such situations.

A gridview in edit mode , can change the label controls to textbox controls --OR -- existing textbox controls in a given row can become editable.

If you run a google , you will get zillion examples.

http://www.google.com/#sclient=psy&hl=en&q=gridview+edit+update+delete&aq=2&aqi=g5&aql=&oq=GridView+edit+update&pbx=1&bav=on.1,or.&fp=eebb40ab4c289d6b

 

[RESOLVED] how do you use css to control look and feel of a datapager for a listview

I have listviews that I would like to modify the look and feel of the default datapager.  How do I do that?

Hello !

We can use listview to view our data by setting its datasource.

Here in the below example we can see how a listview is presented to view a table data :-

 

<LayoutTemplate>
                <div ID="itemPlaceholderContainer" runat="server" style="">
                    <span runat="server" id="itemPlaceholder" />
                </div>
                <div style="">
                    <asp:DataPager ID="DataPager1" runat="server">
                        <Fields>
                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
                                ShowLastPageButton="True" />
                        </Fields>
                    </asp:DataPager>
                </div>
            </LayoutTemplate>

 

Here in the above code we can see that there is a div DataPager control and this div container is having a style property set to value "" emtpy. So if we want to set the css styles for the Datapager here it can be set or any class containing all the styles can be set to teh class name assigning to this div.

 

Think this may be helpfull....

Thanks

[RESOLVED] A Table that is repeated throughout my site: Is this possible

Hi I want to repeat this code throughout my website:

<asp:Table Width="95%" runat="server" HorizontalAlign="Center" CellPadding="10">
            <asp:TableRow>
                <asp:TableCell HorizontalAlign="Center">
                    Copyright Me
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>

 

And then I was trying to put this in a c# file:

 

public const string m_Copyright =
            "<asp:Table Width=\"95%\" runat=\"server\" HorizontalAlign=\"Center\" CellPadding=\"10\">" +
                "<asp:TableRow>" +
                    "<asp:TableCell HorizontalAlign=\"Center\">" +
                        "Copyright Me" +
                    "</asp:TableCell>" +
                "</asp:TableRow>" +
            "</asp:Table>";

 

And then insert this into a aspx page with

 

<%=MyWebsite.GeneralCode.MyConsts.m_Copyright%>

 

But needless to say it does not work. It just shows the 'Copyright me' bit with none of the table alignment.

Is this possible what I am trying or do I have to put the table on each page?

 

Hi,

either place your code on a master page and make use of the master page for every content web form (recommended practice anyway).

Another way is to put the code on a user control and place the user control on those pages where you need it.

Also, I don't know why you're making it a <asp:Table> in the first place. It'll eat resources for nothing. Best is to replace it with a simple <table> element in pure html and use CSS to style it.

Grz, Kris.

Thanks.

Just as a point of interest, when should I use a asp:Table rather than table?

 

jaa17

when should I use a asp:Table rather than table?

Honestly, I don't really remember when I used it myself since 2002 besides demo purposes or so. If you have something like your situation simply go for the <table> html element. If you need to show tabular data coming from some data source (database, xml, rss feed, alien spaceship, ...) simply make use of the data presentation controls like repeater, datalist, listview, gridview, ...

Grz, Kris.

[RESOLVED] Required Fields causing problems in InsertItemTemplate in ListView Control

How can I use required field validators in the insert item template in a listview control?  The empty fields in the insert item template are preventing the user from deleting or updating ay other row.

Mainship

The empty fields in the insert item template are preventing the user from deleting or updating ay other row.

Set CausesValidation="false" for Edit & delete buttons

That works!  But I still have the same problem when editing a record.  Clicking the Update button give me the same problem.  I can't update a record because the insert fields are empty.  Is there a solution for this?

Mainship

But I still have the same problem when editing a record.  Clicking the Update button give me the same problem.  I can't update a record because the insert fields are empty.  Is there a solution for this?

You can set CausesValidation="false" for Update Button also. But that will disable validation for update fileds, which you may want to avoid.

Exactly, so how is this handled?  I'm sure people run into this all the time?

Mainship

Exactly, so how is this handled?  I'm sure people run into this all the time?

You can use Validation groups poperty

Validation groups allow you to organize validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page. You create a validation group by setting the ValidationGroup property to the same name (a string) for all the controls you want to group. You can assign any name to a validation group, but you must use the same name for all members of the group.

Something like this..

<EditItemTemplate>
          <tr style="background-color: #ADD8E6">
            <td>
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" validationgroup="EditGroup" />&nbsp;
              <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" validationgroup="EditGroup" />
            </td>
            <td>
              <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%#Bind("FirstName")  %>'
                MaxLength="50" validationgroup="EditGroup" /><br />
            </td>
            <td>
              <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%#Bind("LastName") %>'
                MaxLength="50" validationgroup="EditGroup" /><br />
            </td>
          </tr>
        </EditItemTemplate>

That does it, thank you!

[RESOLVED] Array List problem.. need help urgent

i'm dealing with a problem where i manage to get all the related data and put it into an arraylist, after that i also manage to display it in a list box.. but, now i was ask to add 30 minutes to all the data in the list box..

how can i do that??

the code i'm using only allow me to add the last item on the list box, other data it will ignore.. 

please help, is urgent.. been struggle with this problem for days T.T

thanks in advanced...

Can you explain this a little more, I'm confused by your requirements.

Can you please provide us your code then we can provide you cause as well as solution for that

 

Hi alicia89,

You can loop the listView, and add minutes for it.

If you want to allow you to do that, you should judge the user' name before you do that.

for(int t=0;t<ListView1.Items.Count;t++)
{
     string text= ListView1.Items[t].Text;

     string value=ListView1.Items[t].Value;

    //Add minutes for the specific item.

     ListView1.Items.Insert(t,new ListItem("newText", "newValue"))
}

If you sitll have problems, you can post your code.