Showing posts with label learn. Show all posts
Showing posts with label learn. Show all posts

Saturday, March 31, 2012

table sizing

I am in my second chapter of trying to learn asp.net and I have my first issue. I am working on an example that asks that I create a HTML table 4x2(RxC) via the web matrix interface. I am not using web matirx, I am with the Visual.NET IDE.
I have inserted a table on the webForm via the HTML tool box, the one under components, and I can not create the 4x2 table. Each time I change the Rows to 4, it keeps getting changed back to 2 when I apply the change.
Why?Vs 03 is notorious for screwing up html code.
Best bet do this:
<table>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
Alternativly i think theres an insert table command on the menu for vs03, which should let you specify 4x2 and insert you a new table.
Or if the book is working from Web Matrix then maybe you should too, after all its a free download from this site :)
Hope this helps,
Andrew
You may be better off using the web form table, not the one on the html portion of the toolbox.
Use the table menu in the top menu bar.
Zath
Thanks, I am learning quickly the quirks of VS.
Thanks.

TableAdapter Insert (Primary Key)

Hi

I have just started to learn ASP.Net. However I have some experience with ASP.

Anyway I have created a asp-form which should create a new entry in the database after you have clicked the submit button.

I created a tableadapter and following simple code:
tbUsersTableAdapter topAdapter=new tbUsersTableAdapter();
topAdapter.InsertUser(DateTime.Now, DateTime.Now, DateTime.Now,true,false, txtNickname.Text, 2, txtEMail.Text, txtPassword.Text,"");

Here is the SQl Command without the primary key:
INSERTINTO[tbUsers] ([CDate], [MDate], [ADate], [Active], [Activated],[Nickname], [Role],Email, [Password],[IP])VALUES (@dotnet.itags.org.CDate, @dotnet.itags.org.MDate, @dotnet.itags.org.ADate, @dotnet.itags.org.Active, @dotnet.itags.org.Activated, @dotnet.itags.org.Nickname, @dotnet.itags.org.Role, @dotnet.itags.org.eMail, @dotnet.itags.org.Password, @dotnet.itags.org.IP)

This works fine as long as I put the ID (Primary Key) into the Insert command. But I don't know how I can find out the next avaiable primary key. I have thought it would be automatically incremented. Anyway I went to the properties of the tableadapter and there was a property which I switched on for my primary key field "AutoIncrement". However it still doesn't work.
Before I used to work with MS Access and OleDB Connection. There I just skipped the Primary Key Value and it has been created automatically. Here I get an error message that the primary key can't be null.

I hope somebody could help me with that or has an idea what I'm doing wrong.

thx

Hi

I searched and tried out a lot of things. Finally I found the problem today. I had to open the table with the Server Explorer. Actually I set my ID field to Primary Key but I forgot to set the Identity Column. After I set the Identity Column to my ID field it worked... great;-)))) Now I can just add a new record with skipping the Primary Key and it will be generated automatically.


thx all

Wednesday, March 28, 2012

TableAdapters and db transactions (begin, comit,roleback....)

Hello

Doing some webapp. in ASP.NET and using som of tutorials from this site

http://www.asp.net/learn/dataaccess/default.aspx?tabid=63 . I havent seen

anything aboat transactions. Is it TableAdapters that runs the show ?

Or if not, where to implement it, becouse whan you to insert data you use a GridView or DetailsView

point to some methode yuo allready made in TableAdapter and thats it. Or am I missing somthing?

Thanks

Its Connection object which handles transactions for you.

SqlTransaction tran = Conn.BeginTransaction();

If you are using DataAdaptor, get the command object,

SqlCommand cmdDelete = DataAdaptor.GetDeleteCommand();
SqlCommand cmdInsert = DataAdaptor.GetInsertCommand();
SqlCommand cmdUpdate = DataAdaptor.GetUpdateCommand();

cmdDelete.Transaction = tran;
cmdInsert.Transaction = tran;
cmdUpdate.Transaction = tran;

Assign this tranaction object to a SQLCommand, use

tran.rollback, to rollbak transactions

tran.commit, to commit transactions

Hope this answers your question.


I think I've got it!!! :-)

ClientTableAdapter clientAdapt =newClientTableAdapter();

SqlConnection myConnection =newSqlConnection("server=(local)\\SQLExpress;Integrated Security=SSPI;database=Novabaza");SqlCommand myCommand =newSqlCommand();SqlTransaction myTrans;// Open the connection.

myConnection.Open();

// Assign the connection property.

myCommand.Connection = myConnection;

// Begin the transaction.

myTrans = myConnection.BeginTransaction();

// Assign transaction object for a pending local transaction

myCommand.Transaction = myTrans;

try

{

// Create a Savepoint

clientAdapt.InsertClientdata(TextBoxName.Text, TextBoxSurname.Text,Convert.ToInt32(TextBoxTelnr.Text));

myTrans.Commit();

Response.Write(

" Records are written to the database!");

}

catch (Exception ex)

{

myTrans.Rollback();

Response.Write("Record is not written to the database!");throw ex;

}

finally

{

myConnection.Close();

}

InsertClientdata() is a query procedure I've made within (Client)tableadapter.

Now I'm goign to try something about using several adapters and creating some "SavePoints" for RollBack

Thanks a lot