I am a newbie, I really want to learn this stuff but I am all confused...
I have a tblNews MSSQL table.. I would like to make an Edit News page... where I select some News from dropdown list and I get the info in a form .. if I select row Koala, show the row that belongs to koala! But it isnt working!
Also, many ppl say use GridView, the automated process.. I tried it, it works, but I cannot learn anything by going the easy way....
I want to know the subject, and thats why I would like to know the hard way....
anyone can give me some advice?? Thank you in advance
Hi, please set breakpoint to “drpNews_SelectedIndexChanged” and debug the codes to check whether the SQL command can the data you want. Additionally, you can write the SQL command to markup:
Response.Write("SELECT * FROM tblNews WHERE ID = " + int.Parse(drpNews.SelectedValue.ToString()));
and then copy and run it with “SQL Server Management Studio” to check whether the command can get the related to records.
in table there are two rows... I should be able to display the appropriate row.. but it just isnt happening...
I also set the dropDownList control to AutoPostBack Enabled....
here is the complete code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
public partial class NewsEdit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated == false)
{
Response.Redirect("hata.aspx");
}
SqlConnection DropDownConn = new SqlConnection(ConfigurationManager.ConnectionStrings["konekcija"].ToString());
SqlCommand DropDownComm = new SqlCommand("SELECT ID, Title FROM tblNews", DropDownConn);
DropDownConn.Open();
SqlDataReader dr = DropDownComm.ExecuteReader();
drpNews.DataSource = dr;
drpNews.DataTextField = "Title";
drpNews.DataValueField = "ID";
drpNews.DataBind();
//drpNews.Items.Insert(0, new ListItem("<Select News>", "0")); // I would also like to set a default value... but it is messed up!
DropDownConn.Close();
}
protected void drpNews_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection slctConn = new SqlConnection(ConfigurationManager.ConnectionStrings["konekcija"].ToString());
SqlCommand slctCmd = new SqlCommand("SELECT * FROM tblNews WHERE ID = " + int.Parse(drpNews.SelectedValue.ToString()), slctConn);
Response.Write(drpNews.SelectedValue.ToString());
slctConn.Open();
SqlDataReader dr2 = slctCmd.ExecuteReader();
if (dr2.Read())
{
txtTitle.Text = dr2["Title"].ToString();
txtShort.Text = dr2["ShortDesc"].ToString();
txtContents.Text = dr2["NewsContent"].ToString();
txtDate.Text = dr2["NewsDate"].ToString().Substring(0, 10);
txtURL.Text = dr2["url"].ToString();
}
slctConn.Close();
}
}
and also, by default, the first row ID is displayed in the DropList
I click on the second ID (row) on the dropdown list it gets me back to the first one!!! and in the text boxes it shows the row data corresponding to the first row, first ID
Regarding my last post: I believe it has something to do with the LOAD PAGE event... I select the data and the page is reloaded again to initilal state... how do I load the page with the data I select from the dropdownlist?
Amiro
Member
1 Points
15 Posts
showing data in form according to DropDown list selection
Aug 19, 2012 01:47 PM|LINK
Hello,
I am a newbie, I really want to learn this stuff but I am all confused...
I have a tblNews MSSQL table.. I would like to make an Edit News page... where I select some News from dropdown list and I get the info in a form .. if I select row Koala, show the row that belongs to koala! But it isnt working!
Also, many ppl say use GridView, the automated process.. I tried it, it works, but I cannot learn anything by going the easy way....
I want to know the subject, and thats why I would like to know the hard way....
anyone can give me some advice?? Thank you in advance
Yours, Amiro
prabu.raveen...
Contributor
5020 Points
955 Posts
Re: showing data in form according to DropDown list selection
Aug 21, 2012 04:44 AM|LINK
Hi here you need to do lots of coding work as follows,
1. Fisrt you need to bind the loaded value into dropdownlist
2. In the selectedindexchanged event of dropdownlist get theselectedvalue into a variable
3. Covert the selected value to its original data
4. Load the Form controls with this selected value
5. To update to the database you have to write seperate code.
6. Reload the dropdownlist after save completed.
Amiro
Member
1 Points
15 Posts
Re: showing data in form according to DropDown list selection
Aug 21, 2012 10:04 AM|LINK
Thank you Prabu, you are very kind and helpful.
Yes I am aware off all that hard work awaiting ... I have already done some things but it isnt working... something is amiss.
I have already created one dropdownList and in SelectedIndexChange I put this code:
protected void drpNews_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection slctConn = new SqlConnection(ConfigurationManager.ConnectionStrings["konekcija"].ToString()); SqlCommand slctCmd = new SqlCommand("SELECT * FROM tblNews WHERE ID = " + int.Parse(drpNews.SelectedValue.ToString()), slctConn); slctConn.Open(); SqlDataReader dr2 = slctCmd.ExecuteReader(); if (dr2.Read()) { txtTitle.Text = dr2["Title"].ToString(); txtShort.Text = dr2["ShortDesc"].ToString(); txtContents.Text = dr2["NewsContent"].ToString(); txtDate.Text = dr2["NewsDate"].ToString().Substring(0, 10); txtURL.Text = dr2["url"].ToString(); } slctConn.Close(); }this is the first step and it is not working :)
thanks
Allen Li - M...
Star
10411 Points
1196 Posts
Re: showing data in form according to DropDown list selection
Aug 22, 2012 08:56 AM|LINK
Hi, please set breakpoint to “drpNews_SelectedIndexChanged” and debug the codes to check whether the SQL command can the data you want. Additionally, you can write the SQL command to markup:
Response.Write("SELECT * FROM tblNews WHERE ID = " + int.Parse(drpNews.SelectedValue.ToString()));and then copy and run it with “SQL Server Management Studio” to check whether the command can get the related to records.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
Amiro
Member
1 Points
15 Posts
Re: showing data in form according to DropDown list selection
Aug 22, 2012 07:26 PM|LINK
Thank you Allen,
I tried it... but it always gives me the ID=1
in table there are two rows... I should be able to display the appropriate row.. but it just isnt happening...
I also set the dropDownList control to AutoPostBack Enabled....
here is the complete code:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Configuration; public partial class NewsEdit : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (User.Identity.IsAuthenticated == false) { Response.Redirect("hata.aspx"); } SqlConnection DropDownConn = new SqlConnection(ConfigurationManager.ConnectionStrings["konekcija"].ToString()); SqlCommand DropDownComm = new SqlCommand("SELECT ID, Title FROM tblNews", DropDownConn); DropDownConn.Open(); SqlDataReader dr = DropDownComm.ExecuteReader(); drpNews.DataSource = dr; drpNews.DataTextField = "Title"; drpNews.DataValueField = "ID"; drpNews.DataBind(); //drpNews.Items.Insert(0, new ListItem("<Select News>", "0")); // I would also like to set a default value... but it is messed up! DropDownConn.Close(); } protected void drpNews_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection slctConn = new SqlConnection(ConfigurationManager.ConnectionStrings["konekcija"].ToString()); SqlCommand slctCmd = new SqlCommand("SELECT * FROM tblNews WHERE ID = " + int.Parse(drpNews.SelectedValue.ToString()), slctConn); Response.Write(drpNews.SelectedValue.ToString()); slctConn.Open(); SqlDataReader dr2 = slctCmd.ExecuteReader(); if (dr2.Read()) { txtTitle.Text = dr2["Title"].ToString(); txtShort.Text = dr2["ShortDesc"].ToString(); txtContents.Text = dr2["NewsContent"].ToString(); txtDate.Text = dr2["NewsDate"].ToString().Substring(0, 10); txtURL.Text = dr2["url"].ToString(); } slctConn.Close(); } }Amiro
Member
1 Points
15 Posts
Re: showing data in form according to DropDown list selection
Aug 22, 2012 07:58 PM|LINK
and also, by default, the first row ID is displayed in the DropList
I click on the second ID (row) on the dropdown list it gets me back to the first one!!! and in the text boxes it shows the row data corresponding to the first row, first ID
weird
Amiro
Member
1 Points
15 Posts
Re: showing data in form according to DropDown list selection
Aug 22, 2012 08:18 PM|LINK
Regarding my last post: I believe it has something to do with the LOAD PAGE event... I select the data and the page is reloaded again to initilal state... how do I load the page with the data I select from the dropdownlist?