date converstion error?
i m using below code
try
{
string date = System.DateTime.Now.ToString();
string bdate = Convert.ToDateTime(TextBox3.Text).ToString("yyyy-MM-dd");
string str;
string g;
g = "";
if (RadioButton1.Checked == true)
{
g = "Male";
}
else
{
g = "Female";
}
FileUpload img = (FileUpload)imgUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
HttpPostedFile File = imgUpload.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
str = "insert into Student values('" + txtName.Text + "','"+bdate.ToString() + "','" + g.ToString() + "','" + txtAddress.Text + "'," + ddlstate.SelectedValue + "," + ddlcity.SelectedValue + ",'" + DropDownList1.SelectedItem.Text + "','" + txtUserName.Text + "','" + TextBox1.Text + "','" + DropDownList2.SelectedItem.Text + "','" + DropDownList3.SelectedItem.Text + "','" + TextBox7.Text + "','"+imgByte.ToString()+"','" + date + "','false')";
Int64 i;
i = ObjConn.ExecuteNonQuery(str);
if
(i > 0)
{
Label1.Text = "Record Inserted Successfully!!!";
Label1.ForeColor = System.Drawing.Color.Green;
txtName.Text = "";
TextBox3.Text = "";
RadioButton1.Checked = false;
RadioButton2.Checked = false;
txtAddress.Text = "";
txtUserName.Text = "";
TextBox1.Text = "";
TextBox7.Text = "";
}
else
{
Label1.Text = "Error while Inserting data!!!";
Label1.ForeColor = System.Drawing.Color.Red;
}
}
catch(Exception ex)
{
throw ex;
}
Sanjay KumarPosted Sep 23, 2013, 7:11 AM
string date = "TextBox3.Text";
string startdated = (Convert.ToDateTime(date)).ToString("yyyy/MM/dd");
or
string startdated = (Convert.ToDateTime(date)).ToString("yyyyMMdd");
VulpesPosted Sep 23, 2013, 6:29 AM
string bdate = Convert.ToDateTime(TextBox3.Text).ToString("yyyy-MM-dd");
to this:
string bdate = Convert.ToDateTime(TextBox3.Text).ToString("yyyyMMdd");
Sanjeeb LenkaPosted Sep 23, 2013, 5:35 AM
i modified your code:
try
{
DateTime date = System.DateTime.Now;
DateTime newDate = DateTime.ParseExact(TextBox3.Text, "dd-MM-yyyy", null);
string s = newDate.ToString("MM-dd-yyyy");
DateTime bdate = Convert.ToDateTime(s);
string str;
string g="";
if (RadioButton1.Checked == true)
{
g = "Male";
}
else
{
g = "Female";
}
FileUpload img = (FileUpload)imgUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
HttpPostedFile File = imgUpload.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
str = "insert into Student values('" + txtName.Text + "','"+bdate + "','" + g.ToString() + "','" + txtAddress.Text + "'," + ddlstate.SelectedValue + "," + ddlcity.SelectedValue + ",'" + DropDownList1.SelectedItem.Text + "','" + txtUserName.Text + "','" + TextBox1.Text + "','" + DropDownList2.SelectedItem.Text + "','" + DropDownList3.SelectedItem.Text + "','" + TextBox7.Text + "','"+imgByte.ToString()+"','" + date + "','false')";
Int64 i;
i = ObjConn.ExecuteNonQuery(str);
if
(i > 0)
{
Label1.Text = "Record Inserted Successfully!!!";
Label1.ForeColor = System.Drawing.Color.Green;
txtName.Text = "";
TextBox3.Text = "";
RadioButton1.Checked = false;
RadioButton2.Checked = false;
txtAddress.Text = "";
txtUserName.Text = "";
TextBox1.Text = "";
TextBox7.Text = "";
}
else
{
Label1.Text = "Error while Inserting data!!!";
Label1.ForeColor = System.Drawing.Color.Red;
}
}
catch(Exception ex)
{
throw ex;
}
venkata kumarPosted Sep 23, 2013, 5:25 AM
try the fallowing
string[] bdate = TextBox3.Text.Split('-');
string bdate=bdate[2]+"-"+bdate[1]+"-"+bdate[0];
Hardik PatelPosted Sep 23, 2013, 5:15 AM
i am getting below error
Sanjeeb LenkaPosted Sep 23, 2013, 5:09 AM
what the format of the date you are selecting in TextBox3.Text. in which line and what error you are getting.