Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Problème de fonction de connexion à une BDD

1 réponse
Avatar
Kalbo
Bonjour,

Actuellement je lis un bouquin pour ne pas le citer « C# 2 et ASP.net 2.0
Developper un projet de A à Z ».
J'essaye donc d'appliquer le modèle MVC du livre pour développer mon site WEB.
Donc j'ai fait une première version de ma BDD et maintenant, je compte
m'attaquer à la couche modal (persistance des données).

La fonction de connexion à la BDD est la suivante :
public DbConnection GetConnection()
{
DbConnection conn = null;
DbProviderFactory myFactory;
try
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ConnectionStringSettings s =
config.ConnectionStrings.ConnectionStrings["FicheConnectionString"];
myFactory = DbProviderFactories.GetFactory(s.ProviderName);
conn = myFactory.CreateConnection();
conn.ConnectionString = s.ConnectionString;
}
catch (DbException)
{
conn = null;
}

return conn;
}

Suivant mon bouquin j’ai ajouté un fichier de configuration (app.config)
dont le code est :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
</configSections>
<connectionStrings>
<add
name="AccesDonneeFiche.Properties.Settings.LivreFicheComConnectionString"
connectionString="Data Source=anthonyb-p\sqlexpress;Initial
Catalog=LivreFicheCom;Persist Security Info=True;User
ID=anthonyb;Password=password"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

Suivant mon bouquin je fais un projet Test (windows form) pour tester cette
fonction.

Code du bouton qui permet de tester la fonction getConnection :
private void button1_Click(object sender, EventArgs e)
{
AccesDonneeFiche.SocleDBFactory myFactory = new
AccesDonneeFiche.SocleDBFactory();
MessageBox.Show(myFactory.GetConnection().ToString());
}

Le problème c'est qu'au lieu d'avoir une MessageBox qui s'affiche
mentionnant "System.Data.SqlClient.SqlConnection", j'ai une exception.

L'exception est :
L'exception System.NullReferenceException n'a pas été gérée
Message="La référence d'objet n'est pas définie à une instance d'un objet."
Source=" AccesDonneeFiche "
StackTrace:
à AccesDonneeFiche.SocleDBFactory.GetConnection() dans
D:\Home\MonAppli\Developpement\Appli\ServiceTechnique\ AccesDonneeFiche
\SocleDBFactory.cs:ligne 40
à PojetTest.Form1.button1_Click(Object sender, EventArgs e) dans
D:\Home\MonAppli\Developpement\Appli\PojetTest\Form1.cs:ligne 22
à System.Windows.Forms.Control.OnClick(EventArgs e)
à System.Windows.Forms.Button.OnClick(EventArgs e)
à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
à System.Windows.Forms.Control.WndProc(Message& m)
à System.Windows.Forms.ButtonBase.WndProc(Message& m)
à System.Windows.Forms.Button.WndProc(Message& m)
à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&
m)
à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
à
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
à
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
à System.Windows.Forms.Application.Run(Form mainForm)
à PojetTest.Program.Main() dans
D:\Home\MonAppli\Developpement\Appli\PojetTest\Program.cs:ligne 17
à System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
à System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
à System.Threading.ThreadHelper.ThreadStart()

Pouvez vous m’aider car je ne comprends pas l’erreur, de plus c’est un
exemple de mon bouquin qui est censé m’aidé à comprendre ?

Merci d’avance pour votre aide,
Kalbo.

1 réponse

Avatar
zoltix
Kalbo a écrit :
Je me suis trombé le code de mon app.config est :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add
name="FicheComConnectionString"
connectionString="Data Source«IDET-P2003sqlexpress;Initial
Catalog=LivreFicheCom;Persist Security Info=True;User
ID=anthonyb;Password=password"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

Merci pour votre aie,
Kalbo.


"Kalbo" a écrit :

Bonjour,

Actuellement je lis un bouquin pour ne pas le citer « C# 2 et ASP.net 2.0
Developper un projet de A à Z ».
J'essaye donc d'appliquer le modèle MVC du livre pour développer mon site WEB.
Donc j'ai fait une première version de ma BDD et maintenant, je compte
m'attaquer à la couche modal (persistance des données).

La fonction de connexion à la BDD est la suivante :
public DbConnection GetConnection()
{
DbConnection conn = null;
DbProviderFactory myFactory;
try
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ConnectionStringSettings s =
config.ConnectionStrings.ConnectionStrings["FicheConnectionString"];
myFactory = DbProviderFactories.GetFactory(s.ProviderName);
conn = myFactory.CreateConnection();
conn.ConnectionString = s.ConnectionString;
}
catch (DbException)
{
conn = null;
}

return conn;
}

Suivant mon bouquin j’ai ajouté un fichier de configuration (app.config)
dont le code est :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
</configSections>
<connectionStrings>
<add
name="AccesDonneeFiche.Properties.Settings.LivreFicheComConnectionString"
connectionString="Data Source=anthonyb-psqlexpress;Initial
Catalog=LivreFicheCom;Persist Security Info=True;User
ID=anthonyb;Password=password"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

Suivant mon bouquin je fais un projet Test (windows form) pour tester cette
fonction.

Code du bouton qui permet de tester la fonction getConnection :
private void button1_Click(object sender, EventArgs e)
{
AccesDonneeFiche.SocleDBFactory myFactory = new
AccesDonneeFiche.SocleDBFactory();
MessageBox.Show(myFactory.GetConnection().ToString());
}

Le problème c'est qu'au lieu d'avoir une MessageBox qui s'affiche
mentionnant "System.Data.SqlClient.SqlConnection", j'ai une exception.

L'exception est :
L'exception System.NullReferenceException n'a pas été gérée
Message="La référence d'objet n'est pas définie à une instance d'un objet."
Source=" AccesDonneeFiche "
StackTrace:
à AccesDonneeFiche.SocleDBFactory.GetConnection() dans
D:HomeMonAppliDeveloppementAppliServiceTechnique AccesDonneeFiche
SocleDBFactory.cs:ligne 40
à PojetTest.Form1.button1_Click(Object sender, EventArgs e) dans
D:HomeMonAppliDeveloppementAppliPojetTestForm1.cs:ligne 22
à System.Windows.Forms.Control.OnClick(EventArgs e)
à System.Windows.Forms.Button.OnClick(EventArgs e)
à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
à System.Windows.Forms.Control.WndProc(Message& m)
à System.Windows.Forms.ButtonBase.WndProc(Message& m)
à System.Windows.Forms.Button.WndProc(Message& m)
à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&
m)
à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
à
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
à
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
à System.Windows.Forms.Application.Run(Form mainForm)
à PojetTest.Program.Main() dans
D:HomeMonAppliDeveloppementAppliPojetTestProgram.cs:ligne 17
à System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
à System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
à System.Threading.ThreadHelper.ThreadStart()

Pouvez vous m’aider car je ne comprends pas l’erreur, de plus c’est un
exemple de mon bouquin qui est censé m’aidé à comprendre ?

Merci d’avance pour votre aide,
Kalb







C'est coolll