In asp.net how do I put the selected value of a gridview to a textbox and how to manually add select

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im a student and aspiring web developer and I somewhat consider myself relatively new to asp.net and I just want to know whats the best way to create/read/update/delelte data from a database.
As of now I know how to do it in two ways:
The first one is by using the sqldatasource which is done by dragging the tables from the server explorer to the page and thus instantly creating the codes automatically, a gridview and sqldatasource objects are then added which will also allows me to
edit the gridview by using its smart panel which then would allow me enable selecting,editing,deleting.....
Ex.
<img src="http://social.microsoft.com/Forums/getfile/4200/" alt="
Its good to have these things done automatically but because I want to learn asp.net I want to learn the different ways of achieving this.
Here is another example that I have but this time I did not use an sqldatasource instead I coded all the statement,SqlDataAdapter and etc. myself
<img src="http://social.microsoft.com/Forums/getfile/4201/" alt="
I know it would not be easy to implement a gridview like my first example manually so I decided to have a gridview then a TextBox/s that will contain the selected value of the cells when I select them. btw here is the code for my second example, I created
a class that contain methods for populating the gridview.

<pre>public GridView LoadCourses(GridView gv)
{
string select = "Select * From Courses";
adapter = new SqlDataAdapter(select, connect);
dt = new DataTable();
adapter.Fill(dt);
gv.DataSource = dt;
gv.DataBind();
return gv;
} SqlClass sc = new SqlClass();
protected void Page_Load(object sender, EventArgs e)
{
sc.LoadCourses(GridView1);
}
protected void Button2_Click(object sender, EventArgs e)
{
sc.AddCourse(TextBox1.Text);
sc.LoadCourses(GridView1);
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

TextBox2.Text = GridView1.SelectedRow.Cells[1].ToString();

}//For the aspx.page here is what I did: <asp:GridView ID="GridView1" runat="server"
onselectedindexchanged="GridView1_SelectedIndexChanged
<Columns><asp:CommandField ButtonType="Button" ShowSelectButton="true" /> </Columns>
<SelectedRowStyle BackColor="Aqua" />
</asp:GridView>
<asp:Label ID="Label2" runat="server" Text="Course name: </asp:Label>
<asp:TextBox ID="TextBox2" runat="server </asp:TextBox>[/code]


My goal is to transfer the course name to TextBox2 when I select from the GridView. I know my question is a bit long but I really want to know more about asp.net
I also want to know which is better, crud with SqlDataSource like in my 1st example or crud by coding it in code behind? I want to learn it so that I can easily seperate the codes from aspx page.

<pre>Right now the value I get on the TextBox2 is = System.Web.UI.WebControls.DataControlFieldCell[/code]


Your answers would be of great help to me. Thank you.
<
Randel Ramirez<br/>
<br/>

View the full article
 
Back
Top