DataRow : problème

Devlop001 Messages postés 18 Date d'inscription mercredi 26 mars 2008 Statut Membre Dernière intervention 11 juin 2008 - 11 avril 2008 à 12:23
mimosa803 Messages postés 345 Date d'inscription jeudi 15 février 2007 Statut Membre Dernière intervention 24 avril 2010 - 11 avril 2008 à 21:51
Bonjour,


J'essaye de puis hier d'ajouter une ligne à ma base de données ( donc un DataRow) pour qu'elle soit affichée dans mon GridView.


L'idée c'est de faire cette ajout en appuyant sur un bouton le plus normalement du monde !


Aucun problème n'est signalé à la compilation. Voici le message d'erreur qui s'affiche en appuyant sur le bouton :


 



La référence d'objet n'est pas définie à une instance d'un objet.




Description : Une exception non gérée s'est produite au moment de l'exécution de la demande Web actuelle. Contrôlez la trace de la pile pour plus d'informations sur l'erreur et son origine dans le code.

Détails de l'exception: System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.

Erreur source:

Ligne 40 :     protected void Button2_Click(object sender, EventArgs e)
Ligne 41 :     {
Ligne 42 :         dr = ds.AgendaDetails.NewRow();
Ligne 43 :         dr[2] = this.TextBox2.Text.ToString();
Ligne 44 :         dr[4] = this.TextBox3.Text.ToString();





 


 



Noter que :  dr est le dataRow ( sans instanciation avec new, refusé par le compilateur !!!?)


                  ds est le DataSet


                  AgendaDetails est le DataTable (la table).


 


 


merci d'avance pour toute intervention. Cordialement.


 


[ S'il y a moyen d'ajouter des ligne directement au GridView merci de me l'indiquer également. ]

3 réponses

mimosa803 Messages postés 345 Date d'inscription jeudi 15 février 2007 Statut Membre Dernière intervention 24 avril 2010
11 avril 2008 à 13:34
    Bonjour, stp montre ton code pourqu'on puisse voir le problème
0
Devlop001 Messages postés 18 Date d'inscription mercredi 26 mars 2008 Statut Membre Dernière intervention 11 juin 2008
11 avril 2008 à 15:51
Ok, Voici donc mon Code : ( pour résumer ce qui pose problème, je dispose de deux TextBox et d'un bouton (entre autres composants...), et d'un GridView lié à une base de donnée.
Je voudrai enrégistrer dans la table AgendaDetails le text saisi dans les deux textbox en appuyant sur le bouton, l'idée donc c'est de dréer un datarow ( une nouvelle ligne) c'est ça justement qui pose problème. )

<%




@



Page



Language
="C#"



AutoEventWireup
="true"



CodeFile
="Default.aspx.cs"




Inherits



="_Default"



Debug
="true"
%>


<!



DOCTYPE



html



PUBLIC



"-//W3C//DTD XHTML 1.1//EN"



"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><


html



xmlns
="http://www.w3.org/1999/xhtml"><


head



runat
="server">






<
title
>
Untitled Page

</
title
></


head
><


body



bgcolor
="infobackground">






<
form



id
="form1"



runat
="server">






<
asp
:
TextBox



ID
="TextBox1"



runat
="server"



AutoCompleteType
="Disabled"



Style
="left: 8px;position: relative; top: 265px; z-index: 100;"





OnTextChanged
="TextBox1_TextChanged"></
asp
:
TextBox
>






<
asp
:
Button



ID
="Button1"



runat
="server"



Style
="left: 42px; position: relative; top: 266px; z-index: 101;"






Text
="+"



Width
="31px"



/>






&nbsp;



&nbsp;&nbsp;






<
div
>






&nbsp;&nbsp;&nbsp;



&nbsp;



&nbsp;



&nbsp;&nbsp;






<
asp
:
Button



ID
="Button3"



runat
="server"



OnClick
="Button3_Click"



Style
="left: 123px;position: relative; top: 241px; z-index: 102;"





Text
="V"



Width
="26px"



/>






<
asp
:
ObjectDataSource



ID
="ObjectDataSource1"



runat
="server"



OldValuesParameterFormatString
="original_{0}"






SelectMethod
="GetLesDates"



TypeName
="DataSet1TableAdapters.ListeDateTableAdapter">






</
asp
:
ObjectDataSource
>






<
asp
:
Button



ID
="Button2"



runat
="server"



Style
="left: 555px;position: relative; top: 237px; z-index: 103;"





Text
="Ajouter"



OnClick
="Button2_Click"



/>






<
asp
:
GridView



ID
="GridView1"



runat
="server"



AllowPaging
="True"



AutoGenerateColumns
="False"






CellPadding
="4"



DataKeyNames
="CodeAgendadetails"



DataSourceID
="ObjectDataSource2"






ForeColor
="#333333"



GridLines
="None"



Style
="z-index: 104; left: 22px; position: absolute;top: 418px"





Width
="606px">






<
FooterStyle



BackColor
="#1C5E55"



Font-Bold
="True"



ForeColor
="White"



/>






<
Columns
>






<
asp
:
CommandField



ButtonType
="Button"



ShowDeleteButton
="True"



ShowEditButton
="True"



/>






<
asp
:
BoundField



DataField
="NomContact"



HeaderText
="NomContact"



SortExpression
="NomContact"



/>






<
asp
:
BoundField



DataField
="Commentaire"



HeaderText
="Commentaire"



SortExpression
="Commentaire"



/>






</
Columns
>






<
RowStyle



BackColor
="#E3EAEB"



/>






<
EditRowStyle



BackColor
="#7C6F57"



/>






<
SelectedRowStyle



BackColor
="#C5BBAF"



Font-Bold
="True"



ForeColor
="#333333"



/>






<
PagerStyle



BackColor
="#666666"



ForeColor
="White"



HorizontalAlign
="Center"



/>






<
HeaderStyle



BackColor
="#1C5E55"



Font-Bold
="True"



ForeColor
="White"



/>






<
AlternatingRowStyle



BackColor
="White"



/>






</
asp
:
GridView
>






<
asp
:
ObjectDataSource



ID
="ObjectDataSource2"



runat
="server"



DeleteMethod
="Delete"






InsertMethod
="Insert"



OldValuesParameterFormatString
="original_{0}"



SelectMethod
="GetData"






TypeName
="DataSet1TableAdapters.AgendaDetailsTableAdapter"



UpdateMethod
="Update">






<
DeleteParameters
>






<
asp
:
Parameter



Name
="Original_CodeAgendadetails"



Type
="Int32"



/>






</
DeleteParameters
>






<
UpdateParameters
>






<
asp
:
Parameter



Name
="LeCode"



Type
="Int32"



/>






<
asp
:
Parameter



Name
="NomContact"



Type
="String"



/>






<
asp
:
Parameter



Name
="HeureContact"



Type
="DateTime"



/>






<
asp
:
Parameter



Name
="Commentaire"



Type
="String"



/>






<
asp
:
Parameter



Name
="Original_CodeAgendadetails"



Type
="Int32"



/>






<
asp
:
Parameter



Name
="CodeAgendadetails"



Type
="Int32"



/>






</
UpdateParameters
>






<
SelectParameters
>






<
asp
:
ControlParameter



ControlID
="TextBox1"



Name
="HeureContact"






PropertyName
="Text"



Type
="DateTime"



/>






</
SelectParameters
>






<
InsertParameters
>






<
asp
:
Parameter



Name
="LeCode"



Type
="Int32"



/>






<
asp
:
Parameter



Name
="NomContact"



Type
="String"



/>






<
asp
:
Parameter



Name
="HeureContact"



Type
="DateTime"



/>






<
asp
:
Parameter



Name
="Commentaire"



Type
="String"



/>






</
InsertParameters
>






</
asp
:
ObjectDataSource
>






<
asp
:
TextBox



ID
="TextBox2"



runat
="server"



Height
="40px"



Style
="z-index: 105; left: 30px;position: absolute; top: 319px"





Width
="82px"></
asp
:
TextBox
>






<
asp
:
TextBox



ID
="TextBox3"



runat
="server"



Height
="40px"



Style
="z-index: 106; left: 141px;position: absolute; top: 320px"





Width
="405px"></
asp
:
TextBox
>






<
asp
:
Calendar



ID
="Calendar1"



runat
="server"



BackColor
="White"



BorderColor
="#3366CC"






BorderWidth
="1px"



CellPadding
="1"



DayNameFormat
="Shortest"



Font-Names
="Verdana"






Font-Size
="8pt"



ForeColor
="#003399"



Height
="200px"



OnSelectionChanged
="Calendar1_SelectionChanged"






Style
="left: 11px; position: relative; top: -66px; z-index: 107;"



Visible
="False"



Width
="220px">






<
SelectedDayStyle



BackColor
="#009999"



Font-Bold
="True"



ForeColor
="#CCFF99"



/>






<
TodayDayStyle



BackColor
="#99CCCC"



ForeColor
="White"



/>






<
SelectorStyle



BackColor
="#99CCCC"



ForeColor
="#336666"



/>






<
WeekendDayStyle



BackColor
="#CCCCFF"



/>






<
OtherMonthDayStyle



ForeColor
="#999999"



/>






<
NextPrevStyle



Font-Size
="8pt"



ForeColor
="#CCCCFF"



/>






<
DayHeaderStyle



BackColor
="#99CCCC"



ForeColor
="#336666"



Height
="1px"



/>






<
TitleStyle



BackColor
="#003399"



BorderColor
="#3366CC"



BorderWidth
="1px"



Font-Bold
="True"






Font-Size
="10pt"



ForeColor
="#CCCCFF"



Height
="25px"



/>






</
asp
:
Calendar
>






&nbsp;&nbsp;






</
div
>






</
form
></


body
></


html
>

VOICI LE CODE BEHINDE  DE LA PAGE :
using

System;
using

System.Data;
using

System.Configuration;
using

System.Web;
using

System.Web.Security;
using

System.Web.UI;
using

System.Web.UI.WebControls;
using

System.Web.UI.WebControls.WebParts;
using

System.Web.UI.HtmlControls;
public

partialclass_Default : System.Web.UI.Page{

DataRow dr;

DataSet1 ds;

DataSet1.
AgendaDetailsDataTable dt;

// DataSet1.AgendaDetailsRow dr;

protected
void Page_Load(
object sender,
EventArgs e){

if (this.IsPostBack false) { TextBox1.Text
DateTime.Now.ToShortDateString(); }}

protected
void TextBox1_TextChanged(
object sender,
EventArgs e){

//this.DropDownList1.Text = this.TextBox1.Text;}

protected
void Button3_Click(
object sender,
EventArgs e){

this.Calendar1.Visible =
true;}

protected
void Calendar1_SelectionChanged(
object sender,
EventArgs e){

this.TextBox1.Text =
this.Calendar1.SelectedDate.ToShortDateString();

this.Calendar1.Visible =
false;}

protected
void Button2_Click(
object sender,
EventArgs e){

dr = dt.NewRow();

dr[2] =

this.TextBox2.Text.ToString();dr[4] =

this.TextBox3.Text.ToString();ds.AgendaDetails.Rows.Add(dr);

//ds.AgendaDetails.Rows.Add(this.TextBox2.Text.ToString(), this.TextBox3.Text.ToString(), null, DateTime.Now);

// ds.AgendaDetails.AddAgendaDetailsRow(this.TextBox2.Text.ToString(), this.TextBox3.Text.ToString(), null, DateTime.Now);

}

}

merci d'avance.
0
mimosa803 Messages postés 345 Date d'inscription jeudi 15 février 2007 Statut Membre Dernière intervention 24 avril 2010
11 avril 2008 à 21:51
    Bonsoir,
D'abord c'est quoi dr = dt.NewRow();
en plus dr tu ne l'as pas instancié
0
Rejoignez-nous