private void fillAds()
{
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
string strQry = "select AD_TYPE,AD_PATH,AD_URL from CP_ADS where SCREEN_ID='1'";
SqlDataAdapter dad = new SqlDataAdapter(strQry, Conn);
DataSet ds = new DataSet();
dad.Fill(ds);
TableRow trtop = new TableRow();
TableCell tctop = new TableCell();
TableRow trleft1 = new TableRow();
TableCell tcleft1 = new TableCell();
TableRow trleft2 = new TableRow();
TableCell tcleft2 = new TableCell();
if (ds.Tables[0].Rows.Count > 0)
{
if (ds.Tables[0].Rows[0]["ad_type"].ToString() == "1")
{
tctop.Controls.Add(generateImage(ds.Tables[0].Rows[0]["AD_PATH"].ToString(), ds.Tables[0].Rows[0]["ad_url"].ToString(), 404, 75));
}
if (ds.Tables[0].Rows.Count >= 3)
{
if (ds.Tables[0].Rows[2]["ad_type"].ToString() == "1")
{
tcleft1.Controls.Add(generateImage(ds.Tables[0].Rows[2]["AD_PATH"].ToString(), ds.Tables[0].Rows[2]["ad_url"].ToString(), 142, 218));
}
}
if (ds.Tables[0].Rows.Count >= 4)
{
if (ds.Tables[0].Rows[3]["ad_type"].ToString() == "1")
{
tcleft2.Controls.Add(generateImage(ds.Tables[0].Rows[3]["AD_PATH"].ToString(), ds.Tables[0].Rows[3]["ad_url"].ToString(), 142, 218));
}
}
if (ds.Tables[0].Rows.Count >0)
{
if (ds.Tables[0].Rows[0]["ad_type"].ToString() == "2")
{
tctop.Text = generateFlash(ds.Tables[0].Rows[0]["AD_PATH"].ToString().Replace("~/", ""), 404, 75); ;
}
}
if (ds.Tables[0].Rows.Count >= 3)
{
if (ds.Tables[0].Rows[2]["ad_type"].ToString() == "2")
{
tcleft1.Text = generateFlash(ds.Tables[0].Rows[2]["AD_PATH"].ToString().Replace("~/", ""), 142, 218); ;
}
}
if (ds.Tables[0].Rows.Count >= 4)
{
if (ds.Tables[0].Rows[3]["ad_type"].ToString() == "2")
{
tcleft2.Text = generateFlash(ds.Tables[0].Rows[3]["AD_PATH"].ToString().Replace("~/", ""), 142, 218); ;
}
}
trtop.Controls.Add(tctop);
trleft1.Controls.Add(tcleft1);
trleft2.Controls.Add(tcleft2);
tblTop.Controls.Add(trtop);
tblLeftQuick1.Controls.Add(trleft1);
tblLeftQuick2.Controls.Add(trleft2);
}
}
Error:
Invalid object name 'CP_ADS'.
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.Data.SqlClient.SqlException: Invalid object name 'CP_ADS'.
Source Error:
Line 124: SqlDataAdapter dad = new SqlDataAdapter(strQry, Conn); Line 125: DataSet ds = new DataSet(); Line 126: dad.Fill(ds); Line 127: TableRow trtop = new TableRow(); Line 128: TableCell tctop = new TableCell();
As far as I see,I think there's something wrong with your sql statement……So plz check whether the columns' names as well as the table names are correctly spelt by pasting the "
select AD_TYPE,AD_PATH,AD_URL from CP_ADS where SCREEN_ID='1'
I was using table name to fetch the data. Now I have changed it to schemaname.table in the statement. It is working fine. But my doubt is why shoould we give schema name for sure? why is it not working with table name?
If I have to make changes to all my sql statement like select * from table to select * from cp.table , what should I do? or do we have any work around for this where all my sql statements consider schema name included by default?
harsharamoju
Member
4 Points
34 Posts
SqlException Was Unhandeld By User Code??? (Invalid Object Name CP_ADS')
Feb 28, 2012 04:00 PM|LINK
private void fillAds() { SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString); string strQry = "select AD_TYPE,AD_PATH,AD_URL from CP_ADS where SCREEN_ID='1'"; SqlDataAdapter dad = new SqlDataAdapter(strQry, Conn); DataSet ds = new DataSet(); dad.Fill(ds); TableRow trtop = new TableRow(); TableCell tctop = new TableCell(); TableRow trleft1 = new TableRow(); TableCell tcleft1 = new TableCell(); TableRow trleft2 = new TableRow(); TableCell tcleft2 = new TableCell(); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["ad_type"].ToString() == "1") { tctop.Controls.Add(generateImage(ds.Tables[0].Rows[0]["AD_PATH"].ToString(), ds.Tables[0].Rows[0]["ad_url"].ToString(), 404, 75)); } if (ds.Tables[0].Rows.Count >= 3) { if (ds.Tables[0].Rows[2]["ad_type"].ToString() == "1") { tcleft1.Controls.Add(generateImage(ds.Tables[0].Rows[2]["AD_PATH"].ToString(), ds.Tables[0].Rows[2]["ad_url"].ToString(), 142, 218)); } } if (ds.Tables[0].Rows.Count >= 4) { if (ds.Tables[0].Rows[3]["ad_type"].ToString() == "1") { tcleft2.Controls.Add(generateImage(ds.Tables[0].Rows[3]["AD_PATH"].ToString(), ds.Tables[0].Rows[3]["ad_url"].ToString(), 142, 218)); } } if (ds.Tables[0].Rows.Count >0) { if (ds.Tables[0].Rows[0]["ad_type"].ToString() == "2") { tctop.Text = generateFlash(ds.Tables[0].Rows[0]["AD_PATH"].ToString().Replace("~/", ""), 404, 75); ; } } if (ds.Tables[0].Rows.Count >= 3) { if (ds.Tables[0].Rows[2]["ad_type"].ToString() == "2") { tcleft1.Text = generateFlash(ds.Tables[0].Rows[2]["AD_PATH"].ToString().Replace("~/", ""), 142, 218); ; } } if (ds.Tables[0].Rows.Count >= 4) { if (ds.Tables[0].Rows[3]["ad_type"].ToString() == "2") { tcleft2.Text = generateFlash(ds.Tables[0].Rows[3]["AD_PATH"].ToString().Replace("~/", ""), 142, 218); ; } } trtop.Controls.Add(tctop); trleft1.Controls.Add(tcleft1); trleft2.Controls.Add(tcleft2); tblTop.Controls.Add(trtop); tblLeftQuick1.Controls.Add(trleft1); tblLeftQuick2.Controls.Add(trleft2); } } Error:Invalid object name 'CP_ADS'.
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.Data.SqlClient.SqlException: Invalid object name 'CP_ADS'.
Source Error:
Source File: c:\CampusPoineer_Praveen_Source_June8_2011\CampusPoineer\Master.master.cs Line: 126
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: SqlException Was Unhandeld By User Code??? (Invalid Object Name CP_ADS')
Mar 01, 2012 01:47 AM|LINK
Hello:)
As far as I see,I think there's something wrong with your sql statement……So plz check whether the columns' names as well as the table names are correctly spelt by pasting the "
into SQL Mangement Studio to have a try with。harsharamoju
Member
4 Points
34 Posts
Re: SqlException Was Unhandeld By User Code??? (Invalid Object Name CP_ADS')
Mar 06, 2012 02:27 AM|LINK
I was using table name to fetch the data. Now I have changed it to schemaname.table in the statement. It is working fine. But my doubt is why shoould we give schema name for sure? why is it not working with table name?
If I have to make changes to all my sql statement like select * from table to select * from cp.table , what should I do? or do we have any work around for this where all my sql statements consider schema name included by default?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: SqlException Was Unhandeld By User Code??? (Invalid Object Name CP_ADS')
Mar 06, 2012 03:36 AM|LINK
What does that mean?
Why do you want to add "cp"?And what's cp?
Show us your screenshot of your SQL table's schema in the SQL Management Studio。