Inserting a command into a database

L

Lars

Hi

I have the following method. HOw do I performe the task to the table in the
database?

protected void FormView1_PageIndexChanging(object sender,
FormViewPageEventArgs e)
{
Button S = sender as Button;

if (S != null)
{
TextBox Name = (TextBox) S.Parent.FindControl("tbName");
TextBox Company = (TextBox) S.Parent.FindControl("tbCompany");
TextBox Email = (TextBox) S.Parent.FindControl("tbEmail");
TextBox Subject = (TextBox) S.Parent.FindControl("tbSubject");
TextBox Message = (TextBox) S.Parent.FindControl("tbMessage");
Label ID = (Label) S.Parent.FindControl("lblID");
SqlDataSource DS = (SqlDataSource)
S.Parent.FindControl("SqlDataSource1");

SqlDataSource1.InsertCommand = "insert into Emails (ID, Name,
Company, Subject, Message ) values ( @ID, @Name, @Company, @Subject,
@Message );";
if ((Name != null) && (Company != null) && (Email != null) &&
(Subject != null) && (Message != null) && (ID != null) &&
(DS != null) )
{
SqlDataSource1.InsertParameters["ID"].DefaultValue = ID.Text
+ 1;
SqlDataSource1.InsertParameters["Name"].DefaultValue =
Name.Text;
SqlDataSource1.InsertParameters["Company"].DefaultValue =
Company.Text;
SqlDataSource1.InsertParameters["Email"].DefaultValue =
Email.Text;
SqlDataSource1.InsertParameters["Subject"].DefaultValue =
Subject.Text;
SqlDataSource1.InsertParameters["Message"].DefaultValue =
Message.Text;
}
SqlDataSource1.
}
}
 
P

Phil H

Hi

I have the following method. HOw do I performe the task to the table in the
database?

    protected void FormView1_PageIndexChanging(object sender,
FormViewPageEventArgs e)
    {
        Button S = sender as Button;

        if (S != null)
        {
            TextBox Name     = (TextBox) S.Parent.FindControl("tbName");
            TextBox Company  = (TextBox) S.Parent.FindControl("tbCompany");
            TextBox Email    = (TextBox) S.Parent.FindControl("tbEmail");
            TextBox Subject  = (TextBox) S.Parent.FindControl("tbSubject");
            TextBox Message  = (TextBox) S.Parent.FindControl("tbMessage");
            Label   ID       = (Label)   S.Parent.FindControl("lblID");
            SqlDataSource DS = (SqlDataSource)
S.Parent.FindControl("SqlDataSource1");

            SqlDataSource1.InsertCommand = "insert into Emails (ID, Name,
Company, Subject, Message ) values ( @ID, @Name, @Company, @Subject,
@Message );";
            if ((Name != null) && (Company != null) && (Email != null) &&
                (Subject != null) && (Message != null) && (ID != null) &&
                (DS != null) )
            {
                SqlDataSource1.InsertParameters["ID"].DefaultValue = ID.Text
+ 1;
                SqlDataSource1.InsertParameters["Name"].DefaultValue =
Name.Text;
                SqlDataSource1.InsertParameters["Company"]..DefaultValue =
Company.Text;
                SqlDataSource1.InsertParameters["Email"].DefaultValue   =
Email.Text;
                SqlDataSource1.InsertParameters["Subject"]..DefaultValue =
Subject.Text;
                SqlDataSource1.InsertParameters["Message"]..DefaultValue =
Message.Text;
            }
            SqlDataSource1.
        }
    }

The concluding statement is just:

SqlDataSource1.Insert();

Anyway why are you doing all this manual coding for a FormView and
SqlDataSource?

When a FormView is attached to a SqlDataSource and all the controls in
the EditItem template are bound to respective table columns,
everything is done automatically when the Insert command is triggered
by a postback from a button whose CommandName property is set to
"Insert".

Are you not aware of this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top