Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 02, 2013 01:00 AM by Decker Dong - MSFT
Member
157 Points
213 Posts
Jan 01, 2013 01:44 PM|LINK
protected void btnCountry_Click(object sender, EventArgs e) { string coun = txtCountry.Text; string query = "insert into countryList (country) values ('" + coun + "')"; dbConnect obj = new dbConnect(); if (obj.execute(query)) { string queryGetData = "select max(id) from countryList"; dbConnect objGetData = new dbConnect(); DataTable dt = objGetData.get(queryGetData); if(dt.Rows.Count>0) { id = dt.Rows[0]["id"].ToString(); ------------------------------------------------------------------------------------------------- error in this line that ...........................Column 'id' does not belong to table . ------------------------------------------------------------------------------------------------- lblCountry.Text = id; } else { Label1.Text = "There is no data"; } } else { lblCountry.Text = "Try again"; } }
All-Star
31832 Points
6516 Posts
Jan 01, 2013 02:20 PM|LINK
584 Points
108 Posts
Jan 01, 2013 02:26 PM|LINK
2 Approaches to to solve this problem.
Change your query to
select max(id) as id from countryList
or
Change the retreviel line to
id = dt.Rows[0][0].ToString();
just for your reference, you can have better approach to get the last inserted Id in more proper way with single execution.
see this http://aspalliance.com/892_CodeSnip_How_to_Get_Id_of_the_Record_Using_ASPNET_and_SQL_Server_2000.all
118619 Points
18779 Posts
Jan 02, 2013 01:00 AM|LINK
Satbir_4 string query = "insert into countryList (country) values ('" + coun + "')";
string query = "insert into countryList (country) values ('" + coun + "')";
Hi,
I don't think you need this,instead,you should use SqlParameter instead to avoid the problem of SQL injection:
protected void btnCountry_Click(object sender, EventArgs e) { string coun = txtCountry.Text; string query = "insert into countryList (country) values (@country)"; //Please use something like SqlComamnd.Parameters.AddWithValue("@country",value); ……………… }
Satbir_4
Member
157 Points
213 Posts
Fetching id of last inserted record
Jan 01, 2013 01:44 PM|LINK
protected void btnCountry_Click(object sender, EventArgs e) { string coun = txtCountry.Text; string query = "insert into countryList (country) values ('" + coun + "')"; dbConnect obj = new dbConnect(); if (obj.execute(query)) { string queryGetData = "select max(id) from countryList"; dbConnect objGetData = new dbConnect(); DataTable dt = objGetData.get(queryGetData); if(dt.Rows.Count>0) { id = dt.Rows[0]["id"].ToString(); ------------------------------------------------------------------------------------------------- error in this line that ...........................Column 'id' does not belong to table . ------------------------------------------------------------------------------------------------- lblCountry.Text = id; } else { Label1.Text = "There is no data"; } } else { lblCountry.Text = "Try again"; } }oned_gk
All-Star
31832 Points
6516 Posts
Re: Fetching id of last inserted record
Jan 01, 2013 02:20 PM|LINK
furry
Member
584 Points
108 Posts
Re: Fetching id of last inserted record
Jan 01, 2013 02:26 PM|LINK
2 Approaches to to solve this problem.
Change your query to
or
Change the retreviel line to
just for your reference, you can have better approach to get the last inserted Id in more proper way with single execution.
see this http://aspalliance.com/892_CodeSnip_How_to_Get_Id_of_the_Record_Using_ASPNET_and_SQL_Server_2000.all
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Fetching id of last inserted record
Jan 02, 2013 01:00 AM|LINK
Hi,
I don't think you need this,instead,you should use SqlParameter instead to avoid the problem of SQL injection:
protected void btnCountry_Click(object sender, EventArgs e) { string coun = txtCountry.Text; string query = "insert into countryList (country) values (@country)"; //Please use something like SqlComamnd.Parameters.AddWithValue("@country",value);……………… }