Click here to Skip to main content
15,888,733 members
Home / Discussions / C#
   

C#

 
GeneralRe: vochancer program Pin
Rahul VB31-Jan-14 6:59
professionalRahul VB31-Jan-14 6:59 
QuestionIf else statemant Pin
manoj s sherje8-Jan-14 19:00
manoj s sherje8-Jan-14 19:00 
AnswerRe: If else statemant Pin
Wayne Gaylard8-Jan-14 19:20
professionalWayne Gaylard8-Jan-14 19:20 
AnswerRe: If else statemant Pin
Bernhard Hiller8-Jan-14 20:29
Bernhard Hiller8-Jan-14 20:29 
AnswerRe: If else statemant Pin
BillWoodruff8-Jan-14 20:35
professionalBillWoodruff8-Jan-14 20:35 
GeneralRe: If else statemant Pin
manoj s sherje8-Jan-14 22:03
manoj s sherje8-Jan-14 22:03 
GeneralRe: If else statemant Pin
OriginalGriff8-Jan-14 22:12
mveOriginalGriff8-Jan-14 22:12 
GeneralRe: If else statemant Pin
V.8-Jan-14 23:50
professionalV.8-Jan-14 23:50 
As someone mentioned
* put the code indication (< pre> tags) around your code.
* use the VB forum for VB questions

my two cents of your code:
good
* use of parametrized queries

bad
* although you use parametrized queries you also still use string concatenation with user input. Here´s a good article[^]
* UI, businesslogic and DAL component code in one class and probably even within one method Dead | X| .
* three times the same code block...
* I added inline comments for more detailed info.

VB
Dim es As Input.KeyEventArgs
If String.IsNullOrEmpty(txt_pname.Text) Then ' good this is better than comparing to ""
comlibobj.msg("Please Enter Name", "Warning")
e.Handled = True
Else
cmd = New SqlCommand("", sqlcn)
cmd.CommandType = CommandType.StoredProcedure
Dim flag As String = ""

'BAD! this allows for SQL injection.
Dim schcmd As New SqlCommand("SELECT * FROM mschemst WHERE scode='" + txt_pcode.Text + "'", sqlcn)
Dim schrd As SqlDataReader = schcmd.ExecuteReader
While schrd.Read
Dim sxm As String = schrd("scode")
If sxm.Count > 0 Then
userCh = "Modify"
End If
' not wrong but you could do something If txt_schfree1.Text = txt_schfree2.Text = txt_schfree3.Text = ... = "0" Then
' but that´s semantics, you are checking this within the while loop though, can´t you take it out and assign a boolean?
If txt_schfree1.Text = "0" And txt_schfree2.Text = "0" And txt_schfree3.Text = "0" And txt_schqty1.Text = "0" And txt_schqty1.Text = "0" And txt_schqty2.Text = "0" And txt_schqty3.Text = "0" Then
userCh = "Delete"
End If
End While
schrd.Close()' what if txt_batchcode.Text is empty or wrong input value or whatever?
If txt_batchcode.Text.Count > 0 Then
Batchno = txt_batchcode.Text
Else
Batchno = Batchno
End If
If userCh = "Add New" Then
flag = "A"
ElseIf userCh = "Modify" Then
flag = "M"
ElseIf userCh = "Delete" Then
flag = "D"
End If
batch = ""
batch = DirectCast(cmb_batch.SelectedItem, ComboBoxItem).Content.ToString()
txtsno.Text = Val(comlibobj.cdmaker("", "mschemst", "sno", 9999))

' again UI stuff where you just touched the database  
If cmb_batch.Visibility = Windows.Visibility.Visible Then
If cmb_batch.Visibility = Windows.Visibility.Visible And cmb_batch.SelectedIndex = 0 Then
System.Windows.MessageBox.Show("Please Select Batch If use Batch Wise Product And enable Select Bacth")
Else
If cmb_batch.SelectedIndex = 1 Then
' OUCH: SQL injection
cmd = New SqlCommand("SELECT BATCHNO FROM mbachmst WHERE BCODE='" + txt_pcode.Text + "'", sqlcn)
rdr = cmd.ExecuteReader
While rdr.Read
Batchno = rdr("BATCHNO").ToString
txtsno.Text = Val(comlibobj.cdmaker("", "mschemst", "sno", 9999))
' this piece of code returns 3 times!  Perhaps a function call instead?
Try
cmd.Parameters.AddWithValue("@flag", flag)
cmd.Parameters.AddWithValue("@sno", txtsno.Text)
cmd.Parameters.AddWithValue("@scode", txt_pcode.Text)
cmd.Parameters.AddWithValue("@SBATCHNO", Batchno)
cmd.Parameters.AddWithValue("@sqty1", txt_schqty1.Text)
cmd.Parameters.AddWithValue("@sfree1", txt_schfree1.Text)
cmd.Parameters.AddWithValue("@sqty2", txt_schqty2.Text)
cmd.Parameters.AddWithValue("@sfree2", txt_schfree2.Text)
cmd.Parameters.AddWithValue("@sqty3", txt_schqty3.Text)
cmd.Parameters.AddWithValue("@sfree3", txt_schfree3.Text)
cmd.Parameters.AddWithValue("@sfromdt", comlibobj.Datecheck(txt_sfromdt.Text))
cmd.Parameters.AddWithValue("@senddt", comlibobj.Datecheck(txt_senddt.Text))
cmd.Parameters.AddWithValue("@smes", txt_smes.Text)
cmd.Parameters.AddWithValue("@sminstk", IIf(txt_minstk.Text = 0, 0, txt_minstk.Text))
cmd.ExecuteNonQuery()
Catch agex As Exception
' users are generally not interested in Exception messages. Log the details and generate a user friendly message.
' alternatively you can say "an error occurred, please provide following details to sysadmin" + agex.Message;
MsgBox(agex.Message)
End Try
cmd.Dispose()
End While
rdr.Close()
Me.Close()
comlibobj.upd()
ElseIf cmb_batch.SelectedIndex = 2 Then
If String.IsNullOrEmpty(txt_batchcode.Text) Then
System.Windows.MessageBox.Show("Batch Cod Field Is blank Please Select Batch")
Else
Try
cmd.Parameters.AddWithValue("@flag", flag)
cmd.Parameters.AddWithValue("@sno", txtsno.Text)
cmd.Parameters.AddWithValue("@scode", txt_pcode.Text)
cmd.Parameters.AddWithValue("@SBATCHNO", Batchno)
cmd.Parameters.AddWithValue("@sqty1", txt_schqty1.Text)
cmd.Parameters.AddWithValue("@sfree1", txt_schfree1.Text)
cmd.Parameters.AddWithValue("@sqty2", txt_schqty2.Text)
cmd.Parameters.AddWithValue("@sfree2", txt_schfree2.Text)
cmd.Parameters.AddWithValue("@sqty3", txt_schqty3.Text)
cmd.Parameters.AddWithValue("@sfree3", txt_schfree3.Text)
cmd.Parameters.AddWithValue("@sfromdt", comlibobj.Datecheck(txt_sfromdt.Text))
cmd.Parameters.AddWithValue("@senddt", comlibobj.Datecheck(txt_senddt.Text))
cmd.Parameters.AddWithValue("@smes", txt_smes.Text)
cmd.Parameters.AddWithValue("@sminstk", IIf(txt_minstk.Text = 0, 0, txt_minstk.Text))
cmd.ExecuteNonQuery()
Catch agex As Exception
MsgBox(agex.Message)
End Try
cmd.Dispose()
Me.Close()
comlibobj.upd() ' is this executed after calling Close?  (could be, not sure)
End If
End If
End If
Else
Try
cmd.Parameters.AddWithValue("@flag", flag)
cmd.Parameters.AddWithValue("@sno", txtsno.Text)
cmd.Parameters.AddWithValue("@scode", txt_pcode.Text)
cmd.Parameters.AddWithValue("@SBATCHNO", Batchno)
cmd.Parameters.AddWithValue("@sqty1", txt_schqty1.Text)
cmd.Parameters.AddWithValue("@sfree1", txt_schfree1.Text)
cmd.Parameters.AddWithValue("@sqty2", txt_schqty2.Text)
cmd.Parameters.AddWithValue("@sfree2", txt_schfree2.Text)
cmd.Parameters.AddWithValue("@sqty3", txt_schqty3.Text)
cmd.Parameters.AddWithValue("@sfree3", txt_schfree3.Text)
cmd.Parameters.AddWithValue("@sfromdt", comlibobj.Datecheck(txt_sfromdt.Text))
cmd.Parameters.AddWithValue("@senddt", comlibobj.Datecheck(txt_senddt.Text))
cmd.Parameters.AddWithValue("@smes", txt_smes.Text)
cmd.Parameters.AddWithValue("@sminstk", IIf(txt_minstk.Text = 0, 0, txt_minstk.Text))
cmd.ExecuteNonQuery()
Catch agex As Exception

MsgBox(agex.Message)
End Try
cmd.Dispose()
Me.Close()
comlibobj.upd()
End If


In short, throw this away and start new, with n-tier development. (use this as base for the logic you tried to achieve)

hope this helps.

GeneralRe: If else statemant Pin
manoj s sherje13-Jan-14 18:10
manoj s sherje13-Jan-14 18:10 
AnswerRe: If else statemant Pin
CPallini8-Jan-14 21:10
mveCPallini8-Jan-14 21:10 
AnswerRe: If else statemant Pin
Rahul VB31-Jan-14 7:06
professionalRahul VB31-Jan-14 7:06 
QuestionSprite Animation and Movement in XNA Pin
incxx8-Jan-14 11:04
incxx8-Jan-14 11:04 
AnswerRe: Sprite Animation and Movement in XNA Pin
Bernhard Hiller8-Jan-14 20:34
Bernhard Hiller8-Jan-14 20:34 
GeneralRe: Sprite Animation and Movement in XNA Pin
incxx10-Jan-14 5:55
incxx10-Jan-14 5:55 
QuestionRFID read and write on sql server using c# Pin
sandsip7-Jan-14 20:50
sandsip7-Jan-14 20:50 
AnswerRe: RFID read and write on sql server using c# Pin
Peter Leow7-Jan-14 21:05
professionalPeter Leow7-Jan-14 21:05 
AnswerRe: RFID read and write on sql server using c# Pin
Eddy Vluggen7-Jan-14 22:32
professionalEddy Vluggen7-Jan-14 22:32 
AnswerRe: RFID read and write on sql server using c# Pin
Marco Bertschi7-Jan-14 23:06
protectorMarco Bertschi7-Jan-14 23:06 
GeneralRe: RFID read and write on sql server using c# Pin
Eddy Vluggen8-Jan-14 0:32
professionalEddy Vluggen8-Jan-14 0:32 
GeneralRe: RFID read and write on sql server using c# Pin
Marco Bertschi8-Jan-14 1:55
protectorMarco Bertschi8-Jan-14 1:55 
AnswerRe: RFID read and write on sql server using c# Pin
David Knechtges8-Jan-14 3:24
David Knechtges8-Jan-14 3:24 
AnswerRe: RFID read and write on sql server using c# Pin
Bernhard Hiller8-Jan-14 20:38
Bernhard Hiller8-Jan-14 20:38 
QuestionNeed help with Floor detection using depth information Pin
erdij7-Jan-14 13:11
professionalerdij7-Jan-14 13:11 
QuestionMessage Closed Pin
7-Jan-14 2:06
professionalMember 104764987-Jan-14 2:06 
SuggestionRe: problem of Deployment c# Application Winforms Pin
Richard Deeming7-Jan-14 2:37
mveRichard Deeming7-Jan-14 2:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.