Jump to content
php.lv forumi

Kļūda C# kodā


DZIEDI

Recommended Posts

Sveiki! Zinu, ka nav gluži php, bet nu varbūt kāds var palīdzēt.

Lūdzu palīdziet atrast kļūdu sekojošā kodā (zem koda ir atgrieztais error`s):

 

<%@ Page Language="C#"%>
<%@ import namespace="System.Data.Odbc" %>
<%
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "SERVER=localhost;" +
"DATABASE=adresati;" +
"UID=lietotajs;" +
"PASSWORD=*****;";
conn.Open();
OdbcCommand cmd = new OdbcCommand();
          cmd.Connection = conn;
          string SQLInsert = "SELECT numurs, zime FROM horoskopu_sanemeji";
          cmd.CommandText = SQLInsert;
OdbcDataReader Reader = cmd.ExecuteReader();
while (Reader.Read())
{
string horoskops = Reader.GetString(1);
OdbcCommand cad = new OdbcCommand();
cad.Connection = conn;
cad.CommandText = "SELECT "+horoskops+" FROM horoskopi WHERE datums='"+DateTime.Now.ToString("MM-dd-yyyy")+"'";
OdbcDataReader Horo = cad.ExecuteReader();
while (Horo.Read())
{
string respmsg = Horo.GetString(0);
}
string destnum = Reader.GetString(0);
string resptype = "SMS:TEXT";
string resp = "{"+resptype+"}{}{}{"+destnum+"}{"+respmsg+"}";
Response.Write(resp);
Horo.Close();
cad.Dispose();
}
Reader.Close();
cmd.Dispose();
conn.Close();
%>

 

Atgrieztā kļūda:

Line 53: string resp = "{"+resptype+"}{}{}{"+destnum+"}{"+respmsg+"}";

Source File: bla-bla-bla\sample.aspx Line: 53

respmsg does not exist in this context

 

Izmēģinātās lietas:

*Gramatikas pārbaude mainīgajos un datubāze

*Datuma funkcijas aizstāšana, to ievadot ar roku

*horoskops sql query`ā ievadīts ar roku

*izveidots atkārtots savienojums otrajam query`am

 

Par googli rakājoties atradu šo:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24558049.html

Negribās viņiem atstāt savas kredītkartes datus lai iegūtu informāciju. Varbūt kāds ir tur reģistrējies.

Link to comment
Share on other sites

Mans minējums:

respmsg tiek definēts while ciklā:

while (Horo.Read())

{

string respmsg = Horo.GetString(0);

}

 

Un, ja gadījumā tas Horo.Read() jau pirmajā iterācijā ir false, tad respmsg vispār netiek definēts.

 

Vai nevajadzētu šādi:

string respmsg = "";

while (Horo.Read())

{

respmsg = Horo.GetString(0);

}

Link to comment
Share on other sites

Horo.Read() nekad nebūs false. Es domāju, ka ārpus while visi mainīgie tiek anulēti un tas ko vajag paturēt ir kaut kā savādāk jānodefinē/jānokonvertē un nezinu ka. Es nedrīkstu definēt respmsg kā "", jo man vajag to atgriezto vērtību.

 

Ierakstot šādi:

while (Horo.Read())
			{
				string respmsg = "atgriezta zina";
			}

Vienalga saņemu šo:

Compiler Error Message: CS0103: The name 'respmsg' does not exist in the current context

Edited by DZIEDI
Link to comment
Share on other sites

Horo.read() būs gan false tad, ka SELECT neatgriež nevienu vērtību. Ja būtu kā Tu saki, ka while cilkā definētie mainīgie tiek anulēti - tad vēl jo vairāk: respmsg tad vispār nekad nebūtu definēts.

Man liekās, ka šitā ir pareizāk:

while (Horo.Read())

{

string respmsg = Horo.GetString(0);

string destnum = Reader.GetString(0);

string resptype = "SMS:TEXT";

string resp = "{"+resptype+"}{}{}{"+destnum+"}{"+respmsg+"}";

Response.Write(resp);

}

 

 

Un kāpēc tiek n-tās reizes taisīts pa jaunu un pēc tam iznīcināts savienojums?

Link to comment
Share on other sites

SELECT vienmēr atgriezīs vienu vērtību (es parūpēšos, lai vienmēr būtu dati, ko atgriezt). Kur tiek veidots un iznīcināts savienojums n-tās reizes?

 

Izmantojot šo:

			while (Reader.Read())
		{
			string horoskops = Reader.GetString(1);;
			OdbcCommand cad = new OdbcCommand();
			cad.Connection = conn;
			cad.CommandText = "SELECT * FROM horoskopi";
			OdbcDataReader Horo = cad.ExecuteReader();
			while (Horo.Read())
			{
				string respmsg = Horo.GetString(4);
				string destnum = Reader.GetString(0);;
				string resptype = "SMS:TEXT";
				string resp = "{"+resptype+"}{}{}{"+destnum+"}{"+respmsg+"}";
				Response.Write(resp);
			}
			Horo.Close();
			cad.Dispose();
		}

Vispār tiek atgriezts kaut kas mistisks:

ERROR [iM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

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.Odbc.OdbcException: ERROR [iM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

 

Source Error:

 

The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

 

1. Add a "Debug=true" directive at the top of the file that generated the error. Example:

 

<%@ Page Language="C#" Debug="true" %>

 

or:

 

2) Add the following section to the configuration file of your application:

 

<configuration>

<system.web>

<compilation debug="true"/>

</system.web>

</configuration>

 

Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.

 

Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.

 

Stack Trace:

 

[OdbcException (0x80131937): ERROR [iM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified]

System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode) +35

System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr, OdbcEnvironmentHandle environmentHandle) +121

System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) +100

System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) +27

System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +49

System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +89

System.Data.Odbc.OdbcConnection.Open() +20

ASP.sample_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) +313

System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +98

System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +21

System.Web.UI.Page.Render(HtmlTextWriter writer) +27

System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +53

System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +280

System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +24

System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +7425

Edited by DZIEDI
Link to comment
Share on other sites

Nu, pag, kāds tad Tev tas skripts izskatās šobrīd? Šitāds tak:

<%@ Page Language="C#"%>
<%@ import namespace="System.Data.Odbc" %>
<%
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "SERVER=localhost;" +
"DATABASE=adresati;" +
"UID=lietotajs;" +
"PASSWORD=*****;";
conn.Open();
OdbcCommand cmd = new OdbcCommand();
          cmd.Connection = conn;
          string SQLInsert = "SELECT numurs, zime FROM horoskopu_sanemeji";
          cmd.CommandText = SQLInsert;
OdbcDataReader Reader = cmd.ExecuteReader();
while (Reader.Read())
                       {
                               string horoskops = Reader.GetString(1);;
                               OdbcCommand cad = new OdbcCommand();
                               cad.Connection = conn;
                               cad.CommandText = "SELECT * FROM horoskopi";
                               OdbcDataReader Horo = cad.ExecuteReader();
                               while (Horo.Read())
                               {
                                       string respmsg = Horo.GetString(4);
                                       string destnum = Reader.GetString(0);;
                                       string resptype = "SMS:TEXT";
                                       string resp = "{"+resptype+"}{}{}{"+destnum+"}{"+respmsg+"}";
                                       Response.Write(resp);
                               }
                               Horo.Close();
                               cad.Dispose();
                       }
Reader.Close();
cmd.Dispose();
conn.Close();
%>

 

Vai ne?

Link to comment
Share on other sites

Man šķiet, ka no šīm rindiņām:

OdbcCommand cad = new OdbcCommand();

cad.Connection = conn;

cad.CommandText = "SELECT * FROM horoskopi";

OdbcDataReader Horo = cad.ExecuteReader()

būtu jāpaliek tikai:

cad.CommandText = "SELECT * FROM horoskopi";

OdbcDataReader Horo = cad.ExecuteReader()

Link to comment
Share on other sites

Tādā gadījumā cad nebūs definēt. Pašā pirmajā no tām rindiņām tiek definēts, kad cad būs ObcdCommand, tālāk tiek noteikt kurš savienojums tiks izmantots, tad pati komanda, proti sql query. It kā neko lieku nesaskatu. mani vēl jo projām tirda tā adrese, kuru norādīju pirmajā postā, meklēju google iespēju atrast to pašu bez reģistrēšanās - nesanāca.

Link to comment
Share on other sites

Beigās izdevās atrisināt problēmu.

Risinājums:

				OdbcCommand cad = new OdbcCommand();
			cad.Connection = conn;
			cad.CommandText = "SELECT "+horoskops+" FROM horoskopi WHERE datums='"+DateTime.Now.ToString("MM-dd-yyyy")+"'<CR><LF>";
			string respmsg = cad.ExecuteScalar().ToString();

un

			string ConnectionString = 
			"Driver={MySQL ODBC 5.1 Driver};"+ //jāieraksta bija draiveris
			"SERVER=localhost;" +
			"DATABASE=adresati;" +
"UID=lietotajs;" +
"PASSWORD=*****;";

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...