Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# and Database Connection String... 1

Status
Not open for further replies.

amorous

Programmer
Sep 5, 2003
1,008
US
Hi Guys...

I have this below connection string...

conn.ConnectionString = "Data Source=(STJSQL\STJDB);" +
"Database=STJData;" +
"User ID=XXX;" +
"Password=XXXXX";

i get the following error:

Compiler Error Message: CS1009: Unrecognized escape sequence

Any ideas...

-VJ

 
you'll have to escape the backslash which is the escape character.

conn.ConnectionString = "Data Source=(STJSQL\\STJDB);" +
"Database=STJData;" +
"User ID=XXX;" +
"Password=XXXXX";

Marty
 
Thanks Marty,

That worked but now when i do this below piece of code..

while(ReaderObj.Read())
{
sItems = (String)ReaderObj.GetValue(1); <-error
iValue = (int)ReaderObj.GetValue(2);
i++;
}


i get this following error:

Exception Details: System.InvalidCastException: Specified cast is not valid.

Any Ideas

The below is my code:
Code:
<script language="C#" runat="server">
String [] sItems;
Int32 [] iValue;
int i = 0; 

void Page_Load(Object Sender, EventArgs e) {             
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=STJSQL\\STJDB;" +
                            "Database=STJData;" +
                            "User ID=XXXX;" +
                            "Password=XXXXXX";

conn.Open();

string sql= "SELECT t.Description as InventName, SUM(CASE WHEN m.Active_Ind =1 THEN 1 ELSE 0 END) as itemtotal FROM Inventory_Main m join Inventory_Type t on m.Inventory_Type_ID=t.Inventory_Type_ID GROUP BY m.Inventory_Type_ID, t.Description ORDER BY t.Description ASC ";

SqlCommand objcommand= new SqlCommand(sql,conn);

SqlDataReader ReaderObj= objcommand.ExecuteReader();

while(ReaderObj.Read()) 
{
sItems[i] = (String)ReaderObj.GetValue(1); 
iValue[i] = (int)ReaderObj.GetValue(2); 
i++; 
}

ReaderObj.Close();
conn.Close(); 


        // Provide a title
        dngchart.ChartTitle  = "<b>Inventory Breakdown:</b>";
        
        // Provide an title for the X-Axis
        dngchart.XAxisTitle  = "(units display actual numbers)";        
 }
</script>

Thank you very much

-VJ
 
why do you cast it? why not try using the functions provided...


while(ReaderObj.Read())
{
sItems = ReaderObj.GetString(1);
iValue = ReaderObj.GetInt32(2);
i++;
}

&quot; ahhh computers, how they made our lives much simpler ;) &quot;
 
Thanks Guys,

djam...

I still get the same error...

Exception Details: System.InvalidCastException: Specified cast is not valid.

Any ideas...

Actually this is my first program in c#...i am not sure whether i am doing it right or wrong....

My Sql query gives me something like this:

InventName itemtotal
________________
computer 34
Monitor 50
Printer 12

so not sure how to populate these values to the arrays...


cappmgr....

I have no idea what you mean by also "it is zero based"
-VJ
 
it starts at zero that is what zerobase means. so your index should be 0 and 1 and you should be all set.
sItems = ReaderObj.GetString(0);
iValue = ReaderObj.GetInt32(1);
 
hmm....new error...

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

Line: sItems = ReaderObj.GetString(0);


Any ideas...

-VJ
 
looks like a null is coming back from the database, just for the heck of it try this to see what you get it won't die if nulls are returned, it is just for debugging

sItems = Convert.ToString(ReaderObj["InventName"]);
iValue = Convert.ToInt32(ReaderObj["itemtotal"]);

good luck I'll look tomorrow.
Marty
 
Nope... same error...

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

and i checked my SQL query...it has no null values...

Thanks...

-VJ
 
Why don't you try a simple case to see if something works in the first place?

string sql= "SELECT * FROM Inventory_Main";

SqlCommand objcommand= new SqlCommand(sql,conn);

SqlDataReader ReaderObj= objcommand.ExecuteReader();

while(ReaderObj.Read())
{
** Print it out **((ReaderObj["InventName"]).ToString());
}

ReaderObj.Close();
conn.Close();

&quot; ahhh computers, how they made our lives much simpler ;) &quot;
 
Hi djam,

Tried this test code:
Code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
void Page_Load(Object Sender, EventArgs e) {             
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=STJSQL\\STJDB;" +
                            "Database=STJData;" +
                            "User ID=XXXX;" +
                            "Password=XXXXX";
conn.Open();
string sql= "SELECT * FROM Inventory_Main" ;
SqlCommand objcommand= new SqlCommand(sql,conn);
SqlDataReader ReaderObj= objcommand.ExecuteReader();
while(ReaderObj.Read()) 
{
((ReaderObj["InventName"]).ToString()); <-error
}
ReaderObj.Close();
conn.Close(); 
 }
</script>
I am getting this below error message:

Compiler Error Message: CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement


Any ideas on whats going wrong...

-VJ
 
But when i used:

ReaderObj.GetString(3); in the while loop

The InventName was the 3rd column so i referred 3 in the GetString Method...

I did not get any error but the result was a blank page...

-VJ
 
Then i tried

Console.WriteLine((ReaderObj["Name"]).ToString());

inside the while loop...still i get the blank page without any errors

-VJ
 
first: you're creating a web application... therefore no Console.Out...
second: if you want to display it on the page use:
Response.Write(ReaderObj.GetString(ReaderObj.GetOrdinal("Name")) + "<br>");
 
Thanks Dazzle...That printed out the names...

but how do i populate this value into a String Array..

Thanks

-VJ
 
Hi Dazzle:

I tried something like this to populate the array:

sItems=ReaderObj.GetString(ReaderObj.GetOrdinal("InventName"));

but i get the following error:

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


I have no idea what to do??

-VJ
 
Ignore this post...i got it working...

-VJ
 
I bet you didn't dimension & instantiate the array -- was that it?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top