hii.....am a beginner in c#.....
am havin a database to store set of dates n t dates should be distinct.
i hav not given t date column has primary key.. codin tat i hav given
below doesnt work properly....
can any one tell me wts wrong in it n how to add distinct dates
protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
dap = new OleDbDataAdapter("select cdate from calen", con);
dap.Fill(ds1);
lbldate.Text = Calendar1.SelectedDate.ToShortDateString();
for (int i = 0; i <= (ds1.Tables[0].Rows.Count -1); i++)
{
if (ds1.Tables[0].Rows[i][0].ToString() == lbldate.Text)
{
lbldate.Text = "date already exists";
}
else
{
OleDbCommand com = new OleDbCommand();
com.CommandText = "insert into calen values('" + lbldate.Text + "')";
com.Connection = con;
com.ExecuteNonQuery();
break;
}
}
}
Loading
shobitha babuPosted Jan 27, 2009, 4:39 AM
desc column name: cdate
datatype:date
i have to insert dates in my table....i tried it wit VB as Code Behind n it is workin fine...
codin in VB
Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar1.SelectionChanged
lbldate.Text = Calendar1.SelectedDate.Date
dap = New OleDb.OleDbDataAdapter("select cdate from calen", con)
dap.Fill(ds)
Dim c = ds.Tables(0).Rows.Count
Dim i As Integer
For i = 0 To (c - 1)
Dim a = ds.Tables(0).Rows(i).Item(0)
If lbldate.Text = a Then
MsgBox("Date Already Exits", MsgBoxStyle.Information, "Warning")
Exit Sub
End If
Next i
com = New OleDb.OleDbCommand
com.CommandText = "insert into calen values('" & Trim(lbldate.Text) & "')"
com.Connection = con
com.ExecuteNonQuery()
End sub
how to do the same in c#
Jan MontanoPosted Jan 27, 2009, 4:24 AM
What exact error are you encountering?
kindly post the schema/structure of your calen table.
My guess is that the data in your INSERT TO statement doesn't properly correspond to the calen table.