Showing posts with label form. Show all posts
Showing posts with label form. Show all posts

Friday, March 23, 2012

Problem With Adding a Date Field to a Database When It is Null

I am having problems adding a date field to a SQL Server Database from a form in ASP.Net. When I leave the date field blank, it automatically inserts Monday, January 01, 1900. I want it to be null when the expiration date is left blank. Can someone please help me with this?
Here's my code for adding information from the table to the database:
'--------------
' name: Button_Click()
'--------------
Sub Button_Click( s As Object, e As EventArgs )



Dim strConnect As String
Dim objConnect As SQLConnection
Dim strInsert As String
Dim cmdInsert As SqlCommand

'Get connection string from Web.Config
strConnect = ConfigurationSettings.AppSettings("ConnectionString")

objConnect = New SqlConnection(strConnect)

strInsert = "Insert DomainName (ClientID, DomainName, Registrar, ExpirationDate ) Values ( @.ClientID, @.DomainName, @.Registrar, @.ExpirationDate )"
cmdInsert = New SqlCommand( strInsert, objConnect)
cmdInsert.Parameters.Add( "@.ClientID", dropClient.SelectedItem.Value )
cmdInsert.Parameters.Add( "@.DomainName", txtDomainName.Text )
cmdInsert.Parameters.Add( "@.Registrar", txtRegistrar.Text )
cmdInsert.Parameters.Add( "@.ExpirationDate", txtExpirationDate.Text )

objConnect.Open()
cmdINsert.ExecuteNonQuery()
objConnect.Close()

'Display the results "page"
DisplayResults()

End Sub
Here's the code for the form:
<form id="frmDomainNames" method="post" runat="server" onSubmit="return InputIsValid()">


<div align="center">
<table border="0" cellpadding="2" cellspacing="2" width="50%" bgcolor="#330099">
<tr>
<td height="37" colspan="2" align="center" valign="middle" bgcolor="#330099"><font color="white" size="5">Domain Name Information</font></td>
<td> </td>
</tr>

<tr>
<td height="42" align="right" valign="top" bgcolor="#e8e8e8"><font face="MS Sans Serif, Arial" size="2" color="#000000"><strong><nobr>
Client's Name:</nobr></strong></font></td>
<td colspan="2" valign="top" bgcolor="#e8e8e8">
<p>
<asp:dropdownlist id="dropClient" runat="server" />
</p>
</td>
</tr>
<tr>
<td height="42" align="right" valign="top" bgcolor="#e8e8e8"><font face="MS Sans Serif, Arial" size="2" color="#000000"><strong><nobr>
Domain Name:</nobr></strong></font></td>
<td colspan="2" valign="top" bgcolor="#e8e8e8">
<p>
<ASP:TextBox id="txtDomainName" runat="server" TextMode="SingleLine" Columns="30" />

</p>
</td>



<tr>
<td height="42" align="right" valign="top" bgcolor="#e8e8e8"><font face="MS Sans Serif, Arial" size="2" color="#000000"><strong><nobr>
Registrar:</nobr></strong></font></td>
<td colspan="2" valign="top" bgcolor="#e8e8e8">
<p>
<ASP:TextBox id="txtRegistrar" runat="server" TextMode="SingleLine" Columns="30" />
</p>
</td>
</tr>
<tr>
<td height="42" align="right" valign="top" bgcolor="#e8e8e8"><font face="MS Sans Serif, Arial" size="2" color="#000000"><strong><nobr>
Expiration Date:</nobr></strong></font></td>
<td colspan="2" valign="top" bgcolor="#e8e8e8">
<p>
<ASP:TextBox id="txtExpirationDate" runat="server" TextMode="SingleLine" Columns="10" />
</p>
</td>
</tr>


<TR>
<TD>
</TD>
<TD align="center">
<asp:Button
Text="Submit"
OnClick="Button_Click"
Runat="Server" />
</TD>
</TR>
</table>
</form>
</div>

Change
cmdInsert.Parameters.Add( "@.ExpirationDate", txtExpirationDate.Text )
to
cmdInsert.Parameters.Add( "@.ExpirationDate", iif(txtExpirationDate.Text="",dbNull.value,txtExpirationDate.text))

Nick

Friday, March 9, 2012

problem when iserting in table

I'm building a form that collects some info from the user (user name, password and type ) and I need to store this info into a table called USER that I created.

I'm using 3 text boxes and a button, when the user clicks the button I need to collect this info and store it into my table

when i click the button i get an error msg in this line (comm.ExecuteNonQuery(); )

what do i need to do?

here is the code

protectedvoid Button1_Click(object sender,EventArgs e)

{

string str ="data source=(local);initial catalog=NH_Project1;integrated security=true;";

SqlConnection conn =newSqlConnection(str);

int rows;

SqlCommand comm =newSqlCommand("INSERT INTO [USER](UserId, UserName, UserPassword, Type)VALUES" +

"@.UsrName,@.UsrPass,@.UsrType", conn);

comm.CommandType =CommandType.Text;

comm.Parameters.AddWithValue("@.UsrName",TextBox1.Text);

comm.Parameters.AddWithValue("@.UsrPass",TextBox2.Text);

comm.Parameters.AddWithValue("@.UsrType", TextBox3.Text);

conn.Open();

rows= comm.ExecuteNonQuery();

conn.Close();

louayazar:

comm.Parameters.AddWithValue("@.UsrName",TextBox1.Text);comm.Parameters.AddWithValue("@.UsrPass",TextBox2.Text);

comm.Parameters.AddWithValue("@.UsrType", TextBox3.Text);

Remove the "@." symbol from your AddWithValue calls. Also, I'd recommend enclosing your ExecuteNonQuery call within a Try/Catch block so you can capture any errors in the future.

|||

Hii

I tried to remove the "@." like what you said but that doesn't work, I still have the same error MSG when clicking the button.

|||

Then make sure to put your ExecuteNonQuery call into a Try/Catch block and see what exception you get. This should help you find out what's going on.

|||

that was helpful, thanks a bunch

Monday, February 20, 2012

problem useing Stored Procedure form vb.net

i have created a stored prcedure but is always give error : and please check the ways is correct using stored procedure

"Procedure or Function 'Add_Cb_Entry' expects parameter '@.Date', which was not supplied."

Dim SqlPrm As SqlParameter

Dim SqlCmd As New SqlCommand

With SqlCmd

.Connection = SqlConnection 'this is my connection setting

.CommandText = "Dbo.Add_Cb_Entry"

.CommandType = CommandType.StoredProcedure

End With

SqlPrm = SqlCmd.Parameters.Add("@.Cashbook_Id", Nothing)

SqlPrm.Direction = ParameterDirection.Output

SqlPrm = SqlCmd.Parameters.Add("@.Date", SqlDbType.DateTime, 8, TxtDate.Text)

SqlPrm.Direction = ParameterDirection.Input

SqlPrm = SqlCmd.Parameters.Add("@.Entry_ID", SqlDbType.Int, 4, "4")

SqlPrm = SqlCmd.Parameters.Add("@.Entry_Name", SqlDbType.VarChar, 75, "Irfan Imdad Memon")

SqlPrm = SqlCmd.Parameters.Add("@.Description", SqlDbType.VarChar, 100, "Chk")

SqlPrm = SqlCmd.Parameters.Add("@.Amount", SqlDbType.Money, 13, "1000.20")

SqlPrm = SqlCmd.Parameters.Add("@.Type", SqlDbType.NChar, 2, "DB")

SqlPrm = SqlCmd.Parameters.Add("@.Entry_Status", SqlDbType.VarChar, 20, "Account")

SqlPrm = SqlCmd.Parameters.Add("@.Ref_No", SqlDbType.Int, 4, "1")

SqlPrm = SqlCmd.Parameters.Add("@.Ref_Status", SqlDbType.VarChar, 20, "MeterialPurchase")

OpenConnection() 'this is my connection setting

SqlCmd.ExecuteNonQuery()

CloseConnection() 'this is my connection setting

TxtInvoiceNo.Text = SqlCmd.Parameters("@.Cashbook_Id").Value

hi
when you use this statement:

SqlPrm = SqlCmd.Parameters.Add("@.Cashbook_Id", Nothing)

sql server not found Nothing !
you set this parameter with zero or any varchar.

good luck

Problem updating SQL2005 From MSAccess Form

I have recently migrated from SQL2K to SQL2K5 and have a number of front ends in MSAccess Forms that worked fine with SQL2K but are not working with SQL2K5. I am able to connect and view the data in the SQL table using a file DSN but I am unable to update the records from the MSAccess form in SQL2K5. I have checked all of the permissions and have even given the Login dbo privs. I have also allowed remote access through the Surface Area Configuration Tool. What am I missing?

Thanks in advance for any assistance you can provide.

What are you using as the front end?, Access Project or Access mdb? If it's a Project I don't think they can update in SS2005

I've just completed a big Access/SQL Server 2005 project, leaving the front end as an mdb. There are a few quirks, you can't use Views only SP's and pass through queries, but it still works well.

|||I am using Access mdb. Sql tables are Linked tables in the Access mdb. Thanks for the advice. I'll ensure that there are no views involved.|||You should run one of your queries from the query design grid, try to make a change to the returned result set, if you can make an update , you know it's a forms based problem not permissions.|||Same result using the Design Grid. I am able to display the contents of the table but unable to update or append records from the grid. WHen I try a simple update query I get the error "Operation must use an updatable query".|||Problem solved. For some reason when I originally created the Linked Table I was not prompted to identify a unique key in the table for updating. Recreating the Linked Table with the unique fields identified solved the problem.